#!/home/python/.virtualenvs/open/bin/python

import sys
sys.path.append('./')
sys.path.insert(0, "../")

import os
if not os.getenv('DJANGO_SETTINGS_MODULE'):
    os.environ['DJANGO_SETTINGS_MODULE'] = 'equity_mall.settings.dev'

import django, socket
django.setup()


from celery import shared_task
import logging, json, datetime, base64, requests, random, time
from random import randint, shuffle
from decimal import Decimal
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
from django.db import transaction
from django.db.models import Q, F
from django.utils import timezone
from elasticsearch import Elasticsearch, helpers

from orders.models import OrderInfo, FundBill, VoucherDetail, SYOrderInfo, OrderGoods, ResidentialHostelOrderInfo, OrderSchengle, OrderSchengleCoupons
from goods.models import GoodsCoupons, LaunchedGoods, Goods, GoodsImage
from vouchers.models import AliDiscountCouponInfo, AliDiscountCouponBillDetails, CustomizeActivityRules, CustomizeActivityFlipCARDS, AliDiscountCoupon, CustomizeActivity
from vouchers.utils import AlipayOpen
from customers.utils import AlipayOpen as C_AlipayOpen
from merchants.models import MerchantBill, Merchant, MerchantChannel, MerchantUser, MerchantImage, MerchantBindCardOrder, MerchantBusinessQRCode
from equity_mall.utils.gzh_send_templates import ali_fundtrans_template, invite_push_notification
from equity_mall.utils.synchronization_kl_order import pull_orderinfo, pull_goods, pull_user, pull_userMobile, check_shop_can_subsidy, pull_orderinfo1
from activities.models import Channel
from users.models import Level, CustomerUser, CustomerUserVice, CustomerUserViceInviterRecord
from equity_mall.utils.sync_img_to_local import sync_img_to_local
from businesses.models import BindCardOrder, get_point_record_db_model
from customers.utils import pay_transactions_refund, query_transactions_refund
from cashbonus.models import CashActivityRule, CashActivity, CashActivityData
from equity_mall.utils.utils import random_str
from equity_mall.utils.utils import redis_baidu_token, chack_identify, mmpaymkttransfers, redis_scheme_token, mmpaymkttransfers_xh, encrypt_pkcs7_padding
from businesses.utils import cancel_group_order
from customers.models import *
from orders.utils import schengle
from equity_mall.utils.sync_img_to_local import make_qrcode1, make_qrcode4

logger = logging.getLogger('django')


@shared_task
def dispose_ali_order_result(data):
    print(data)
    TRADE_STATUS_ENUM = {
        'WAIT_BUYER_PAY': 1,
        'TRADE_CLOSED': 2,
        'TRADE_SUCCESS': 3,
        'TRADE_FINISHED': 4
    }
    fund_bill_list = data.pop('fund_bill_list', None)
    voucher_detail_list = data.pop('voucher_detail_list', None)
    order_info = OrderInfo.objects.get(order_id=data['out_trade_no'])
    if data['trade_status'] == 'TRADE_SUCCESS':
        order_info.state = 12
        order_info.payment_time = data['gmt_payment']
        order_info.trade_no = data['trade_no']
        order_info.realpay_amount = data['invoice_amount']
        order_info.save()
        ordergoods_set = order_info.ordergoods_set.all()
        for ordergoods in ordergoods_set:
            if ordergoods.goods.category_id == 2:
                goodscoupons_set = GoodsCoupons.objects.filter(goods_id=ordergoods.goods_id, status=0)[:ordergoods.count]
                for goodscoupons in goodscoupons_set:
                    goodscoupons.order_id = order_info.id
                    goodscoupons.status = 1
                    goodscoupons.save()
            elif ordergoods.goods.category_id == 1:
                for i in range(ordergoods.count):
                    while True:
                        coupons_id = '%16d' % randint(0, 9999999999999999)
                        goodscoupons = GoodsCoupons.objects.filter(coupons_id=coupons_id)
                        if not goodscoupons:
                            break
                    GoodsCoupons.objects.create(
                        coupons_id=coupons_id,
                        status=1,
                        goods_id=ordergoods.goods_id,
                        order_id=order_info.id,
                        validity_time=datetime.datetime.now()
                    )
            # try:
            #     if ordergoods.goods.is_settlement == 1:
            #         receipts_time = datetime.datetime.now().strftime('%Y-%m-%d')
            #         bill = MerchantBill.objects.filter(receipts_time=receipts_time, merchant_id=ordergoods.merchant_id).first()
            #         if not bill:
            #             bill_data = {}
            #             bill_data['merchant_id'] = ordergoods.merchant_id
            #             bill_data['receipts_time'] = receipts_time
            #             bill_data['point'] = ordergoods.point
            #             bill_data['amount'] = ordergoods.price
            #             bill_data['payable_amount'] = ordergoods.price + Decimal(int(ordergoods.point) / 100)
            #             bill, create = MerchantBill.objects.get_or_create(defaults=bill_data,
            #                                                               receipts_time=receipts_time,
            #                                                               merchant_id=ordergoods.merchant_id)
            #         else:
            #             bill.point = bill.point + ordergoods.point
            #             bill.amount = bill.amount + ordergoods.price
            #             bill.payable_amount = bill.payable_amount + Decimal(int(ordergoods.point) / 100) + ordergoods.price
            #             bill.save()
            #         order_info.payable_settled_amount = order_info.payable_settled_amount + Decimal(int(ordergoods.point) / 100) + ordergoods.price
            #         order_info.bill_id = bill.id
            #         order_info.save()
            # except Exception as e:
            #     print(e)
            #     pass
        try:
            orderfundtrans_set = order_info.orderfundtrans_set.all().filter(status=1)
            for orderfundtrans in orderfundtrans_set:
                alipay_obj = C_AlipayOpen()
                trans_amount = str(orderfundtrans.exchange_amount)
                order_title = orderfundtrans.order_title
                out_biz_no = orderfundtrans.out_biz_no
                identity = orderfundtrans.identity
                if orderfundtrans.identity_type == 1:
                    result = json.loads(alipay_obj.transunitransfer_user_id(identity, out_biz_no, trans_amount, order_title))
                else:
                    name = orderfundtrans.name
                    result = json.loads(alipay_obj.transunitransfer_login_id(identity, name, out_biz_no, trans_amount, order_title))
                if result['code'] != '10000':
                    orderfundtrans.sub_msg = result['sub_msg']
                    orderfundtrans.status = 4
                    orderfundtrans.save()
                    try:
                        ali_fundtrans_template(order_info.id, str(trans_amount), result['sub_msg'])
                    except:
                        pass
                else:
                    orderfundtrans.ali_order_id = result['order_id']
                    orderfundtrans.pay_fund_order_id = result['pay_fund_order_id']
                    orderfundtrans.trans_date = result['trans_date']
                    orderfundtrans.status = 2
                    orderfundtrans.save()
        except Exception as e:
            pass
    if fund_bill_list:
        FUND_CHANNEL_ENUM = {
            'COUPON': 1,
            'ALIPAYACCOUNT': 2,
            'POINT': 3,
            'DISCOUNT': 4,
            'PCARD': 5,
            'FINANCEACCOUNT': 6,
            'MCARD': 7,
            'MDISCOUNT': 8,
            'MCOUPON': 9,
            'PCREDIT': 10
        }
        fund_bill_list = json.loads(fund_bill_list)
        for fund_bill_data in fund_bill_list:
            # fund_bill_data = json.loads(fund_bill_data)
            fund_bill_data['fundChannel'] = FUND_CHANNEL_ENUM[fund_bill_data['fundChannel']]
            fund_bill_data['ali_order_id'] = order_info.id
            FundBill.objects.create(**fund_bill_data)
    if voucher_detail_list:
        voucher_detail_list = json.loads(voucher_detail_list)
        for voucher_detail_data in voucher_detail_list:
            voucher_detail_data['v_type'] = voucher_detail_data.pop('type', None)
            voucher_detail_data['ali_order_id'] = order_info.id
            VoucherDetail.objects.create(**voucher_detail_data)
    try:
        vouchers = AliDiscountCouponInfo.objects.filter(status__in=[1, 8], customer_user_id=order_info.customer_user_id, alidiscountcoupon__coupon_channel=1)
        alipayopen = AlipayOpen()
        for voucher in vouchers:
            activityinfo = voucher.alidiscountcoupon
            result = json.loads(alipayopen.voucherquery(voucher.voucher_id))
            if result['status'] == 'USED':
                activityinfo.use_count += 1
                try:
                    if 'bill_details' in result:
                        for bill_datail in result['bill_details']:
                            voucher.gmt_use = bill_datail['gmt_create']
                            bill_datail['voucherinfo_id'] = voucher.id
                            AliDiscountCouponBillDetails.objects.create(**bill_datail)
                    activityinfo.save()
                except:
                    pass
                try:
                    alidiscountcoupon_level = voucher.alidiscountcoupon_level
                    alidiscountcoupon_level.use_number += 1
                    alidiscountcoupon_level.p_use_number += 1
                    alidiscountcoupon_level.save()
                    relation_set = alidiscountcoupon_level.child_set.all()
                    for relation in relation_set:
                        parent = relation.parent
                        parent.p_use_number += 1
                        parent.save()
                except:
                    pass
            voucher.status = voucher.STATUS_ENUM[result['status']]
            voucher.save()
        alidiscounponinfo_set = order_info.alidiscountcouponinfo_set.all().filter(status=8)
        for voucher in alidiscounponinfo_set:
            activityinfo = voucher.alidiscountcoupon
            activityinfo.use_count += 1
            activityinfo.save()
            voucher.gmt_use = datetime.datetime.now()
            voucher.status = 7
            voucher.save()
            try:
                alidiscountcoupon_level = voucher.alidiscountcoupon_level
                alidiscountcoupon_level.use_number += 1
                alidiscountcoupon_level.p_use_number += 1
                alidiscountcoupon_level.save()
                relation_set = alidiscountcoupon_level.child_set.all()
                for relation in relation_set:
                    parent = relation.parent
                    parent.p_use_number += 1
                    parent.save()
                voucher_detail_data = {}
                voucher_detail_data['v_type'] = 'COUPON'
                voucher_detail_data['voucherId'] = voucher.voucher_id
                voucher_detail_data['name'] = voucher.name
                voucher_detail_data['otherContribute'] = order_info.total_price - order_info.payable_amount
                voucher_detail_data['merchantContribute'] = 0
                voucher_detail_data['ali_order_id'] = order_info.id
                voucher_detail_data['amount'] = order_info.total_price - order_info.payable_amount
                VoucherDetail.objects.create(**voucher_detail_data)
            except:
                pass
            coupon_bill_data = {}
            coupon_bill_data['amount'] = order_info.total_price
            coupon_bill_data['voucherinfo_id'] = voucher.id
            coupon_bill_data['biz_type'] = 'V_USE'
            coupon_bill_data['gmt_create'] = datetime.datetime.now()
            coupon_bill_data['status'] = 'S'
            coupon_bill_data['trade_no'] = data['trade_no']
            AliDiscountCouponBillDetails.objects.create(**coupon_bill_data)
    except:
        pass
    return '写入成功'


@shared_task
def dispose_wx_order_result(data):
    ciphertext = data['resource']['ciphertext']
    nonce = data['resource']['nonce']
    associated_data = data['resource']['associated_data']
    key = 'xinxiangxinxiangxinxiangxinxiang'
    key_bytes = str.encode(key)
    nonce_bytes = str.encode(nonce)
    ad_bytes = str.encode(associated_data)
    ciphertext_bytes = base64.b64decode(ciphertext)
    aesgcm = AESGCM(key_bytes)
    order_data = json.loads(aesgcm.decrypt(nonce_bytes, ciphertext_bytes, ad_bytes).decode())
    try:
        order_info = OrderInfo.objects.get(order_id=order_data.pop('out_trade_no'))
    except Exception as e:
        logging.error(e)
        return '未能找到对应订单号订单'
    promotion_detail = order_data.pop('promotion_detail', None)
    if order_data['trade_state'] == 'SUCCESS':
        if order_info.order_type != 4:
            if order_info.order_type == 1:
                order_info.state = 2
            else:
                order_info.state = 12
            order_info.payment_time = order_data['success_time'].replace('T', ' ').replace('+08:00', '')
            order_info.trade_no = order_data['transaction_id']
            order_info.realpay_amount = Decimal(str(order_data['amount']['payer_total']))/100
            order_info.save()
            try:
                fund_bill_data = {}
                if 'OTHERS' in order_data['bank_type']:
                    fundChannel = 11
                elif 'DEBIT' in order_data['bank_type']:
                    fundChannel = 13
                elif 'CREDIT' in order_data['bank_type']:
                    fundChannel = 12
                else:
                    fundChannel =14
                fund_bill_data['fundChannel'] = fundChannel
                fund_bill_data['ali_order_id'] = order_info.id
                fund_bill_data['amount'] = Decimal(str(order_data['amount']['payer_total']))/100
                fund_bill_data['bank_code'] = order_data['bank_type']
                FundBill.objects.create(**fund_bill_data)
            except:
                pass
            ordergoods_set = order_info.ordergoods_set.all()
            for ordergoods in ordergoods_set:
                if ordergoods.goods.category_id == 2:
                    goodscoupons_set = GoodsCoupons.objects.filter(goods_id=ordergoods.goods_id, status=0)[:ordergoods.count]
                    for goodscoupons in goodscoupons_set:
                        goodscoupons.order_id = order_info.id
                        goodscoupons.status = 1
                        goodscoupons.save()
                elif ordergoods.goods.category_id == 1:
                    for i in range(ordergoods.count):
                        while True:
                            coupons_id = '%16d' % randint(0, 9999999999999999)
                            goodscoupons = GoodsCoupons.objects.filter(coupons_id=coupons_id)
                            if not goodscoupons:
                                break
                        GoodsCoupons.objects.create(
                            coupons_id=coupons_id,
                            status=1,
                            goods_id=ordergoods.goods_id,
                            order_id=order_info.id,
                            validity_time=datetime.datetime.now()
                        )
                # try:
                #     if ordergoods.goods.is_settlement == 1:
                #         receipts_time = datetime.datetime.now().strftime('%Y-%m-%d')
                #         bill = MerchantBill.objects.filter(receipts_time=receipts_time, merchant_id=ordergoods.merchant_id).first()
                #         if not bill:
                #             bill_data = {}
                #             bill_data['merchant_id'] = ordergoods.merchant_id
                #             bill_data['receipts_time'] = receipts_time
                #             bill_data['point'] = ordergoods.point
                #             bill_data['amount'] = ordergoods.price
                #             bill_data['payable_amount'] = ordergoods.price + Decimal(int(ordergoods.point) / 100)
                #             bill, create = MerchantBill.objects.get_or_create(defaults=bill_data,
                #                                                               receipts_time=receipts_time,
                #                                                               merchant_id=ordergoods.merchant_id)
                #         else:
                #             bill.point = bill.point + ordergoods.point
                #             bill.amount = bill.amount + ordergoods.price
                #             bill.payable_amount = bill.payable_amount + Decimal(int(ordergoods.point) / 100) + ordergoods.price
                #             bill.save()
                #         order_info.payable_settled_amount = order_info.payable_settled_amount + Decimal(int(ordergoods.point) / 100) + ordergoods.price
                #         order_info.bill_id = bill.id
                #         order_info.save()
                # except:
                #     pass
            try:
                orderfundtrans_set = order_info.orderfundtrans_set.all().filter(status=1)
                for orderfundtrans in orderfundtrans_set:
                    alipay_obj = C_AlipayOpen()
                    trans_amount = str(orderfundtrans.exchange_amount)
                    order_title = orderfundtrans.order_title
                    out_biz_no = orderfundtrans.out_biz_no
                    identity = orderfundtrans.identity
                    if orderfundtrans.identity_type == 1:
                        result = json.loads(alipay_obj.transunitransfer_user_id(identity, out_biz_no, trans_amount, order_title))
                    else:
                        name = orderfundtrans.name
                        result = json.loads(alipay_obj.transunitransfer_login_id(identity, name, out_biz_no, trans_amount, order_title))
                    if result['code'] != '10000':
                        orderfundtrans.sub_msg = result['sub_msg']
                        orderfundtrans.status = 4
                        orderfundtrans.save()
                        try:
                            ali_fundtrans_template(order_info.id, str(trans_amount), result['sub_msg'])
                        except:
                            pass
                    else:
                        orderfundtrans.ali_order_id = result['order_id']
                        orderfundtrans.pay_fund_order_id = result['pay_fund_order_id']
                        orderfundtrans.trans_date = result['trans_date']
                        orderfundtrans.status = 2
                        orderfundtrans.save()
            except Exception as e:
                pass
            try:
                if promotion_detail:
                    TYPE_ENUM = {
                        'CASH': 1,
                        'NOCASH': 2
                    }
                    SCOPE_ENUM = {
                        'GLOBAL': 1,
                        'SINGLE': 2
                    }
                    for i in promotion_detail:
                        voucher_detail_data = {}
                        type = i.pop('type', None)
                        wechatpay_contribute = i.pop('wechatpay_contribute', None)
                        merchant_contribute = i.pop('merchant_contribute', None)
                        voucher_detail_data['v_type'] = type
                        voucher_detail_data['voucherId'] = i['coupon_id']
                        voucher_detail_data['name'] = i['name']
                        if wechatpay_contribute:
                            voucher_detail_data['otherContribute'] = Decimal(str(wechatpay_contribute)) / 100
                        else:
                            voucher_detail_data['otherContribute'] = 0
                        if merchant_contribute:
                            voucher_detail_data['merchantContribute'] = Decimal(str(merchant_contribute)) / 100
                        else:
                            voucher_detail_data['merchantContribute'] = 0
                        voucher_detail_data['ali_order_id'] = order_info.id
                        voucher_detail_data['amount'] = Decimal(str(i['amount'])) / 100
                        VoucherDetail.objects.create(**voucher_detail_data)
                        fund_bill_data = {}
                        if type:
                            if type == 'CASH':
                                fundChannel = 4
                            else:
                                fundChannel = 8
                        else:
                            fundChannel = 14
                        fund_bill_data['fundChannel'] = fundChannel
                        fund_bill_data['ali_order_id'] = order_info.id
                        fund_bill_data['amount'] = Decimal(str(Decimal(str(i['amount'])) / 100)) / 100
                        fund_bill_data['bank_code'] = order_data['bank_type']
                        FundBill.objects.create(**fund_bill_data)
            except:
                pass
            try:
                alidiscounponinfo_set = order_info.alidiscountcouponinfo_set.all().filter(status=8)
                for voucher in alidiscounponinfo_set:
                    activityinfo = voucher.alidiscountcoupon
                    activityinfo.use_count += 1
                    activityinfo.save()
                    voucher.gmt_use = datetime.datetime.now()
                    voucher.status = 7
                    voucher.save()
                    try:
                        alidiscountcoupon_level = voucher.alidiscountcoupon_level
                        alidiscountcoupon_level.use_number += 1
                        alidiscountcoupon_level.p_use_number += 1
                        alidiscountcoupon_level.save()
                        relation_set = alidiscountcoupon_level.child_set.all()
                        for relation in relation_set:
                            parent = relation.parent
                            parent.p_use_number += 1
                            parent.save()
                        voucher_detail_data = {}
                        voucher_detail_data['v_type'] = 'COUPON'
                        voucher_detail_data['voucherId'] = voucher.voucher_id
                        voucher_detail_data['name'] = voucher.name
                        voucher_detail_data['otherContribute'] = order_info.total_price - order_info.payable_amount
                        voucher_detail_data['merchantContribute'] = 0
                        voucher_detail_data['ali_order_id'] = order_info.id
                        voucher_detail_data['amount'] = order_info.total_price - order_info.payable_amount
                        VoucherDetail.objects.create(**voucher_detail_data)
                    except:
                        pass
                    coupon_bill_data = {}
                    coupon_bill_data['amount'] = order_info.total_price
                    coupon_bill_data['voucherinfo_id'] = voucher.id
                    coupon_bill_data['biz_type'] = 'V_USE'
                    coupon_bill_data['gmt_create'] = datetime.datetime.now()
                    coupon_bill_data['status'] = 'S'
                    coupon_bill_data['trade_no'] = data['trade_no']
                    AliDiscountCouponBillDetails.objects.create(**coupon_bill_data)
            except:
                pass
        else:
            if 'PSBC' not in order_data['bank_type']:
                order_info.state = 8
                data_json = {}
                out_refund_no = timezone.now().strftime('%Y%m%d%H%M%S') + ('%06d' % randint(0, 999999)) + ('%09d' % order_info.customer_user_id)
                data_json['out_trade_no'] = order_info.order_id
                data_json['out_refund_no'] = out_refund_no
                data_json['notify_url'] = 'https://api.sutpay.com/cpay/refund_result/'
                data_json['amount'] = {"refund": int(order_data['amount']['payer_total']), "total": int(order_data['amount']['payer_total']), "currency": "CNY"}
                data_dict = pay_transactions_refund(data_json)
                order_info.out_refund_no = out_refund_no
                order_info.payment_time = order_data['success_time'].replace('T', ' ').replace('+08:00', '')
                order_info.trade_no = order_data['transaction_id']
                order_info.realpay_amount = Decimal(str(order_data['amount']['payer_total'])) / 100
                order_info.save()
                try:
                    alidiscounponinfo_set = order_info.alidiscountcouponinfo_set.all().filter(status=8)
                    for voucher in alidiscounponinfo_set:
                        voucher.status = 1
                        voucher.save()
                except:
                    pass
                ordergoods_set = order_info.ordergoods_set.all()
                for ordergoods in ordergoods_set:
                    launchedgoods = ordergoods.launched_goods
                    launched_origin_stock = launchedgoods.stock
                    launched_origin_sales = launchedgoods.sales
                    launched_new_stock = launched_origin_stock + ordergoods.count
                    launched_new_sales = launched_origin_sales - ordergoods.count
                    launchedgoods.stock = launched_new_stock
                    launchedgoods.sales = launched_new_sales
                    launchedgoods.save()
            else:
                order_info.state = 2
                order_info.payment_time = order_data['success_time'].replace('T', ' ').replace('+08:00', '')
                order_info.trade_no = order_data['transaction_id']
                order_info.realpay_amount = Decimal(str(order_data['amount']['payer_total'])) / 100
                order_info.save()
                try:
                    fund_bill_data = {}
                    if 'OTHERS' in order_data['bank_type']:
                        fundChannel = 11
                    elif 'DEBIT' in order_data['bank_type']:
                        fundChannel = 13
                    elif 'CREDIT' in order_data['bank_type']:
                        fundChannel = 12
                    else:
                        fundChannel = 14
                    fund_bill_data['fundChannel'] = fundChannel
                    fund_bill_data['ali_order_id'] = order_info.id
                    fund_bill_data['amount'] = Decimal(str(order_data['amount']['payer_total'])) / 100
                    fund_bill_data['bank_code'] = order_data['bank_type']
                    FundBill.objects.create(**fund_bill_data)
                except:
                    pass
                ordergoods_set = order_info.ordergoods_set.all()
                for ordergoods in ordergoods_set:
                    if ordergoods.goods.category_id == 2:
                        goodscoupons_set = GoodsCoupons.objects.filter(goods_id=ordergoods.goods_id, status=0)[
                                           :ordergoods.count]
                        for goodscoupons in goodscoupons_set:
                            goodscoupons.order_id = order_info.id
                            goodscoupons.status = 1
                            goodscoupons.save()
                    elif ordergoods.goods.category_id == 1:
                        for i in range(ordergoods.count):
                            while True:
                                coupons_id = '%16d' % randint(0, 9999999999999999)
                                goodscoupons = GoodsCoupons.objects.filter(coupons_id=coupons_id)
                                if not goodscoupons:
                                    break
                            GoodsCoupons.objects.create(
                                coupons_id=coupons_id.replace(' ', ''),
                                status=1,
                                goods_id=ordergoods.goods_id,
                                order_id=order_info.id,
                                validity_time='2023-07-31 23:59:59'
                            )
                try:
                    if promotion_detail:
                        TYPE_ENUM = {
                            'CASH': 1,
                            'NOCASH': 2
                        }
                        SCOPE_ENUM = {
                            'GLOBAL': 1,
                            'SINGLE': 2
                        }
                        for i in promotion_detail:
                            voucher_detail_data = {}
                            type = i.pop('type', None)
                            wechatpay_contribute = i.pop('wechatpay_contribute', None)
                            merchant_contribute = i.pop('merchant_contribute', None)
                            voucher_detail_data['v_type'] = type
                            voucher_detail_data['voucherId'] = i['coupon_id']
                            voucher_detail_data['name'] = i['name']
                            if wechatpay_contribute:
                                voucher_detail_data['otherContribute'] = Decimal(str(wechatpay_contribute)) / 100
                            else:
                                voucher_detail_data['otherContribute'] = 0
                            if merchant_contribute:
                                voucher_detail_data['merchantContribute'] = Decimal(str(merchant_contribute)) / 100
                            else:
                                voucher_detail_data['merchantContribute'] = 0
                            voucher_detail_data['ali_order_id'] = order_info.id
                            voucher_detail_data['amount'] = Decimal(str(i['amount'])) / 100
                            VoucherDetail.objects.create(**voucher_detail_data)
                            fund_bill_data = {}
                            if type:
                                if type == 'CASH':
                                    fundChannel = 4
                                else:
                                    fundChannel = 8
                            else:
                                fundChannel = 14
                            fund_bill_data['fundChannel'] = fundChannel
                            fund_bill_data['ali_order_id'] = order_info.id
                            fund_bill_data['amount'] = Decimal(str(Decimal(str(i['amount'])) / 100)) / 100
                            fund_bill_data['bank_code'] = order_data['bank_type']
                            FundBill.objects.create(**fund_bill_data)
                except:
                    pass
                try:
                    alidiscounponinfo_set = order_info.alidiscountcouponinfo_set.all().filter(status=8)
                    for voucher in alidiscounponinfo_set:
                        activityinfo = voucher.alidiscountcoupon
                        activityinfo.use_count += 1
                        activityinfo.save()
                        voucher.gmt_use = datetime.datetime.now()
                        voucher.status = 7
                        voucher.save()
                        try:
                            voucher_detail_data = {}
                            voucher_detail_data['v_type'] = 'COUPON'
                            voucher_detail_data['voucherId'] = voucher.voucher_id
                            voucher_detail_data['name'] = voucher.name
                            voucher_detail_data['otherContribute'] = order_info.total_price - order_info.payable_amount
                            voucher_detail_data['merchantContribute'] = 0
                            voucher_detail_data['ali_order_id'] = order_info.id
                            voucher_detail_data['amount'] = order_info.total_price - order_info.payable_amount
                            VoucherDetail.objects.create(**voucher_detail_data)
                        except:
                            pass
                        coupon_bill_data = {}
                        coupon_bill_data['amount'] = order_info.total_price
                        coupon_bill_data['voucherinfo_id'] = voucher.id
                        coupon_bill_data['biz_type'] = 'V_USE'
                        coupon_bill_data['gmt_create'] = datetime.datetime.now()
                        coupon_bill_data['status'] = 'S'
                        coupon_bill_data['trade_no'] = data['trade_no']
                        AliDiscountCouponBillDetails.objects.create(**coupon_bill_data)
                except:
                    pass
    return '写入成功'


@shared_task
def dispose_cmf_order_result(data):
    try:
        order_info = OrderInfo.objects.get(order_id=data['orderId'])
    except Exception as e:
        logging.error(e)
        return '未能找到对应订单号订单'
    if data['result_code'] == 'SUCCESS':
        if order_info.order_type in [1, 7]:
            # order_info.state = 2
            if order_info.order_type == 1:
                order_info.state = 2
            elif order_info.order_type == 7:
                order_info.state = 15
                group = order_info.group
                group.group_status = 1
                group.save()
            else:
                order_info.state = 12
            order_info.payment_time = datetime.datetime.strptime(data['timeEnd'], '%Y%m%d%H%M%S')
            order_info.trade_no = data['channel_trade_no']
            order_info.realpay_amount = Decimal(str(data['money'])) - Decimal(str(data['activity_money']))
            order_info.freight = Decimal(str(data['commission']))
            order_info.save()
            if order_info.order_type == 7:
                try:
                    cancel_group_order(group)
                except:
                    pass
            ordergoods_set = order_info.ordergoods_set.all()
            for ordergoods in ordergoods_set:
                if ordergoods.goods.category_id == 2:
                    goodscoupons_set = GoodsCoupons.objects.filter(goods_id=ordergoods.goods_id, status=0)[:ordergoods.count]
                    for goodscoupons in goodscoupons_set:
                        goodscoupons.order_id = order_info.id
                        goodscoupons.status = 1
                        goodscoupons.save()
                elif ordergoods.goods.category_id == 1:
                    for i in range(ordergoods.count):
                        while True:
                            coupons_id = '%16d' % randint(0, 9999999999999999)
                            coupons_id = coupons_id.replace(' ', '')
                            goodscoupons = GoodsCoupons.objects.filter(coupons_id=coupons_id)
                            if not goodscoupons:
                                break
                        GoodsCoupons.objects.create(
                            coupons_id=coupons_id,
                            status=1,
                            goods_id=ordergoods.goods_id,
                            order_id=order_info.id,
                            validity_time=datetime.datetime.now()
                        )
            try:
                orderfundtrans_set = order_info.orderfundtrans_set.all().filter(status=1)
                for orderfundtrans in orderfundtrans_set:
                    alipay_obj = C_AlipayOpen()
                    trans_amount = str(orderfundtrans.exchange_amount)
                    order_title = orderfundtrans.order_title
                    out_biz_no = orderfundtrans.out_biz_no
                    identity = orderfundtrans.identity
                    if orderfundtrans.identity_type == 1:
                        result = json.loads(alipay_obj.transunitransfer_user_id(identity, out_biz_no, trans_amount, order_title))
                    else:
                        name = orderfundtrans.name
                        result = json.loads(alipay_obj.transunitransfer_login_id(identity, name, out_biz_no, trans_amount, order_title))
                    if result['code'] != '10000':
                        orderfundtrans.sub_msg = result['sub_msg']
                        orderfundtrans.status = 4
                        orderfundtrans.save()
                        try:
                            ali_fundtrans_template(order_info.id, str(trans_amount), result['sub_msg'])
                        except:
                            pass
                    else:
                        orderfundtrans.ali_order_id = result['order_id']
                        orderfundtrans.pay_fund_order_id = result['pay_fund_order_id']
                        orderfundtrans.trans_date = result['trans_date']
                        orderfundtrans.status = 2
                        orderfundtrans.save()
            except Exception as e:
                pass
            try:
                alidiscounponinfo_set = order_info.alidiscountcouponinfo_set.all().filter(status=8)
                for voucher in alidiscounponinfo_set:
                    activityinfo = voucher.alidiscountcoupon
                    activityinfo.use_count += 1
                    activityinfo.save()
                    voucher.gmt_use = datetime.datetime.now()
                    voucher.status = 7
                    voucher.save()
                    try:
                        alidiscountcoupon_level = voucher.alidiscountcoupon_level
                        alidiscountcoupon_level.use_number += 1
                        alidiscountcoupon_level.p_use_number += 1
                        alidiscountcoupon_level.save()
                        relation_set = alidiscountcoupon_level.child_set.all()
                        for relation in relation_set:
                            parent = relation.parent
                            parent.p_use_number += 1
                            parent.save()
                        voucher_detail_data = {}
                        voucher_detail_data['v_type'] = 'COUPON'
                        voucher_detail_data['voucherId'] = voucher.voucher_id
                        voucher_detail_data['name'] = voucher.name
                        voucher_detail_data['otherContribute'] = order_info.total_price - order_info.payable_amount
                        voucher_detail_data['merchantContribute'] = 0
                        voucher_detail_data['ali_order_id'] = order_info.id
                        voucher_detail_data['amount'] = order_info.total_price - order_info.payable_amount
                        VoucherDetail.objects.create(**voucher_detail_data)
                    except:
                        pass
                    coupon_bill_data = {}
                    coupon_bill_data['amount'] = order_info.total_price
                    coupon_bill_data['voucherinfo_id'] = voucher.id
                    coupon_bill_data['biz_type'] = 'V_USE'
                    coupon_bill_data['gmt_create'] = datetime.datetime.now()
                    coupon_bill_data['status'] = 'S'
                    coupon_bill_data['trade_no'] = data['trade_no']
                    AliDiscountCouponBillDetails.objects.create(**coupon_bill_data)
            except:
                pass
        elif order_info.order_type == 5:
            order_info.payment_time = datetime.datetime.strptime(data['timeEnd'], '%Y%m%d%H%M%S')
            order_info.trade_no = data['channel_trade_no']
            order_info.realpay_amount = Decimal(str(data['money'])) - Decimal(str(data['activity_money']))
            order_info.freight = Decimal(str(data['commission']))
            order_info.state = 9
            order_info.save()
        elif order_info.order_type == 3:
            order_info.payment_time = datetime.datetime.strptime(data['timeEnd'], '%Y%m%d%H%M%S')
            order_info.trade_no = data['channel_trade_no']
            order_info.realpay_amount = Decimal(str(data['money'])) - Decimal(str(data['activity_money']))
            order_info.freight = Decimal(str(data['commission']))
            order_info.state = 2
            order_info.save()
            receipts_time = order_info.payment_time.strftime('%Y-%m-%d')
            bill = MerchantBill.objects.filter(receipts_time=receipts_time, merchant_id=order_info.merchant_id, bill_type=order_info.order_species).first()
            if not bill:
                bill_data = {}
                bill_data['merchant_id'] = order_info.merchant_id
                bill_data['receipts_time'] = receipts_time
                bill_data['amount'] = order_info.total_price
                bill_data['commission'] = order_info.freight
                bill_data['level_id'] = order_info.merchant.level_id
                bill_data['payable_amount'] = order_info.total_price - order_info.freight
                bill_data['channel_id'] = 3
                bill_data['bill_type'] = order_info.order_species
                bill, create = MerchantBill.objects.get_or_create(defaults=bill_data, receipts_time=receipts_time, merchant_id=order_info.merchant_id, bill_type=order_info.order_species)
            else:
                bill.amount = bill.amount + order_info.total_price
                bill.commission = bill.commission + order_info.freight
                bill.payable_amount = bill.payable_amount + order_info.total_price - order_info.freight
                bill.save()
            order_info.payable_settled_amount = order_info.total_price - order_info.freight
            order_info.bill_id = bill.id
            order_info.save()
            sd_order = OrderSchengle.objects.filter(order_id=order_info.id).first()
            if sd_order:
                data = {
                    "source": "GDYZQY",
                    "outTradeNo": order_info.order_id,
                    "formCode": sd_order.formCode,
                    "username": order_info.customeruservice.realname
                }
                result = schengle.create_order(data)
                if result['resultCode'] == 'success':
                    sd_order.orderId = result['data']['orderId']
                    sd_order.create_time = result['data']['createTime']
                    sd_order.start_time = result['data']['startTime']
                    sd_order.end_time = result['data']['endTime']
                    sd_order.order_state = 2
                    sd_order.save()
                    couponInfoList = result['data']['couponInfoList']
                    for couponInfo in couponInfoList:
                        goods_id = order_info.ordergoods_set.all().first().goods_id
                        identify = random_str()
                        coupon_qrcode = f'youhuiquanyi/goodscoupon/goodscoupon_{goods_id}/{identify}.png'
                        coupon_qrcode_url = make_qrcode1(identify, couponInfo['matrixContent'], coupon_qrcode)
                        goodscoupons_data = {}
                        goodscoupons_data['coupons_id'] = couponInfo['couponId']
                        goodscoupons_data['status'] = 1
                        goodscoupons_data['goods_id'] = goods_id
                        goodscoupons_data['order_id'] = order_info.id
                        goodscoupons_data['coupons_qrcode'] = coupon_qrcode
                        goodscoupons_data['validity_time'] = couponInfo['endTime']
                        GoodsCoupons.objects.get_or_create(defaults=goodscoupons_data, coupons_id=couponInfo['couponId'])
                        orderschenglecoupons_data = {}
                        orderschenglecoupons_data['order_id'] = order_info.id
                        orderschenglecoupons_data['couponId'] = couponInfo['couponId']
                        orderschenglecoupons_data['serviceId'] = couponInfo['serviceId']
                        orderschenglecoupons_data['matrixContent'] = couponInfo['matrixContent']
                        orderschenglecoupons_data['createTime'] = couponInfo['createTime']
                        orderschenglecoupons_data['startTime'] = couponInfo['startTime']
                        orderschenglecoupons_data['endTime'] = couponInfo['endTime']
                        orderschenglecoupons_data['coupon_state'] = 1
                        OrderSchengleCoupons.objects.get_or_create(defaults=orderschenglecoupons_data, couponId=couponInfo['couponId'])
                else:
                    sd_order.resultDesc = result['resultDesc']
                    sd_order.order_state = 3
                    sd_order.save()
    return '写入成功'


@shared_task
def dispose_wxpay_order_result(data):
    ciphertext = data['resource']['ciphertext']
    nonce = data['resource']['nonce']
    associated_data = data['resource']['associated_data']
    key = 'xinxiangxinxiangxinxiangxinxiang'
    key_bytes = str.encode(key)
    nonce_bytes = str.encode(nonce)
    ad_bytes = str.encode(associated_data)
    ciphertext_bytes = base64.b64decode(ciphertext)
    aesgcm = AESGCM(key_bytes)
    order_data = json.loads(aesgcm.decrypt(nonce_bytes, ciphertext_bytes, ad_bytes).decode())
    try:
        order_info = BindCardOrder.objects.get(out_trade_no=order_data.pop('out_trade_no'))
    except Exception as e:
        logging.error(e)
        return '未能找到对应订单号订单'
    if order_data['trade_state'] == 'SUCCESS':
        order_info.success_time = order_data['success_time'].replace('T', ' ').replace('+08:00', '')
        order_info.trade_no = order_data['transaction_id']
        order_info.transaction_id = order_data['transaction_id']
        order_info.bank_type = order_data['bank_type']
        if order_data['bank_type'] == 'PSBC_DEBIT':
            order_info.bank_type_sign = 1
        elif order_data['bank_type'] == 'PSBC_CREDIT':
            order_info.bank_type_sign = 2
        else:
            order_info.bank_type_sign = 3
        order_info.order_status = 2
        order_info.prize_status = 6
        order_info.save()
        data_json = {}
        data_json['out_trade_no'] = order_info.out_trade_no
        data_json['out_refund_no'] = order_info.out_refund_no
        data_json['notify_url'] = 'https://api.sutpay.com/cpay/refund_result/'
        data_json['amount'] = {"refund": 1, "total": 1, "currency": "CNY"}
        data_dict = pay_transactions_refund(data_json)
        if 'code' in data_dict:
            order_info.message = data_dict['message']
            order_info.order_status = 5
            order_info.save()
        if 'PSBC' not in order_data['bank_type']:
            order_info.prize_status = 5
            order_info.save()
        else:
            if not order_info.customizeactivityflipcards_id:
                customizeactivity = order_info.customizeactivity
                check_customizeactivityrules_set = customizeactivity.customizeactivityrules_set.filter(Q(alidiscountcoupon__remain_number__gt=0) | Q(cashactivity__remain_amount__gt=0))
                list_data = []
                for check_customizeactivityrules in check_customizeactivityrules_set:
                    probability = int(check_customizeactivityrules.count)
                    for i in range(probability):
                        list_data.append(check_customizeactivityrules.id)
                customizeactivityrules_id = random.choices(list_data, k=1)[0]
                customizeactivityrules = CustomizeActivityRules.objects.get(id=customizeactivityrules_id)
                identify = f'100003{str(datetime.datetime.now()).replace("-", "").replace(":", "").replace(" ", "").replace(".", "")}{"%06d" % randint(0, 999999)}'
                instance_data = dict()
                instance_data['identify'] = identify
                instance_data['customeruservice_id'] = order_info.customeruservice_id
                instance_data['mobile'] = order_info.customeruservice.mobile
                instance_data['gears'] = 1
                instance_data['customizeactivity_id'] = customizeactivity.id
                if customizeactivityrules.prize_type in [1, 2]:
                    instance_data['alidiscountcoupon_id'] = customizeactivityrules.alidiscountcoupon_id
                    instance_data['win_type'] = customizeactivityrules.prize_type
                elif customizeactivityrules.prize_type == 3:
                    instance_data['cashactivity_id'] = customizeactivityrules.cashactivity_id
                    instance_data['win_type'] = customizeactivityrules.prize_type
                instance = CustomizeActivityFlipCARDS.objects.create(**instance_data)
                if instance.win_type in [1, 2]:
                    instance.open_status = 2
                    instance.open_time = datetime.datetime.now()
                    instance.save()
                    alidiscountcoupon = instance.alidiscountcoupon
                    if alidiscountcoupon.remain_number <= 0:
                        x = list(customizeactivity.customizeactivityrules_set.filter(
                            alidiscountcoupon__remain_number__gt=0).values_list('alidiscountcoupon_id', flat=True))
                        if x:
                            shuffle(x)
                            alidiscountcoupon = AliDiscountCoupon.objects.get(id=x[0])
                            instance.alidiscountcoupon_id = alidiscountcoupon.id
                            instance.save()
                        else:
                            instance.open_status = 3
                            instance.win_type = 4
                            instance.open_time = datetime.datetime.now()
                            instance.save()
                            return '奖品库存不足,请稍后再试'
                    activity_id = alidiscountcoupon.id
                    while True:
                        alidiscountcoupon = AliDiscountCoupon.objects.get(id=activity_id)
                        activity_origin_send_count = alidiscountcoupon.send_count
                        activity_origin_remain_number = alidiscountcoupon.remain_number
                        activity_new_send_count = activity_origin_send_count + 1
                        activity_new_remain_number = activity_origin_remain_number - 1
                        if activity_new_remain_number < 0:
                            instance.open_status = 3
                            instance.win_type = 4
                            instance.open_time = datetime.datetime.now()
                            instance.save()
                            return '奖品库存不足,请稍后再试'
                        result = AliDiscountCoupon.objects.filter(id=activity_id,send_count=activity_origin_send_count).update(send_count=activity_new_send_count, remain_number=activity_new_remain_number)
                        if result == 0:
                            continue
                        break
                    alidiscountcouponinfo_data = {}
                    alidiscountcouponinfo_data['alidiscountcoupon_id'] = alidiscountcoupon.id
                    alidiscountcouponinfo_data['level_id'] = instance.level_id
                    alidiscountcouponinfo_data['status'] = 0
                    alidiscountcouponinfo_data['template_id'] = alidiscountcoupon.template_id
                    out_biz_no = f'100004{str(datetime.datetime.now()).replace("-", "").replace(":", "").replace(" ", "").replace(".", "")}{"%06d" % randint(0, 999999)}'
                    alidiscountcouponinfo_data['out_biz_no'] = out_biz_no
                    alidiscountcouponinfo_data['gmt_distributed'] = datetime.datetime.now()
                    alidiscountcouponinfo_data['customeruservice_id'] = order_info.customeruservice_id
                    alidiscountcouponinfo_data['coupon_channel'] = alidiscountcoupon.coupon_channel
                    alidiscountcouponinfo, crete = AliDiscountCouponInfo.objects.get_or_create(defaults=alidiscountcouponinfo_data, out_biz_no=out_biz_no)
                    instance.alidiscountcouponinfo_id = alidiscountcouponinfo.id
                    instance.save()
                else:
                    instance.open_status = 2
                    instance.open_time = datetime.datetime.now()
                    instance.save()
                    cashactivity = instance.cashactivity
                    cashactivityrule = CashActivityRule.objects.filter(cash_activity_id=cashactivity.id, is_delete=0).first()
                    if cashactivityrule.amount > cashactivity.remain_amount:
                        instance.open_status = 3
                        instance.win_type = 4
                        instance.open_time = datetime.datetime.now()
                        instance.save()
                        return '奖品库存不足,请稍后再试'
                    amount = cashactivityrule.amount
                    activity_id = cashactivity.id
                    while True:
                        activity = CashActivity.objects.get(id=activity_id)
                        activity_origin_take_amount = activity.take_amount
                        activity_origin_remain_amount = activity.remain_amount
                        if activity_origin_remain_amount < amount:
                            instance.open_status = 3
                            instance.win_type = 4
                            instance.open_time = datetime.datetime.now()
                            instance.save()
                            return '奖品库存不足,请稍后再试'
                        activity_new_take_amount = activity_origin_take_amount + amount
                        activity_new_remain_amount = activity_origin_remain_amount - amount
                        result = CashActivity.objects.filter(take_amount=activity_origin_take_amount, remain_amount=activity_origin_remain_amount, id=activity_id).update(
                            take_amount=activity_new_take_amount, remain_amount=activity_new_remain_amount)
                        if result == 0:
                            continue
                        break
                    date_now = datetime.datetime.now()
                    activity_data = {}
                    activity_data['amount'] = amount
                    activity_data['create_time'] = date_now
                    activity_data['cash_activity_id'] = activity.id
                    activity_data['level_id'] = instance.level_id
                    activity_data['state'] = 1
                    identify = random_str()
                    nonce_str = random_str()
                    activity_data['identify'] = identify
                    activity_data['nonce_str'] = nonce_str
                    activity_data['desc'] = activity.name
                    activity_data['partner_trade_no'] = f'{date_now.strftime("%Y%m%d%H%M%S")}{"%06d" % randint(0,999999)}{"%07d" % order_info.customeruservice_id}'
                    activity_data['expired_time'] = date_now + datetime.timedelta(hours=11)
                    activity_dat, create = CashActivityData.objects.get_or_create(defaults=activity_data, identify=identify)
                    instance.cashactivitydata_id = activity_dat.id
                    instance.amount = amount
                    instance.save()
                order_info.customizeactivityflipcards_id = instance.id
                order_info.save()
                # customeruservice = order_info.customeruservice
                # customeruservice.is_psbc_card = 1
                # customeruservice.save()
            try:
                if order_info.business_qrcode and order_info.is_exchange == 0:
                    business_qrcode = order_info.business_qrcode
                    instance = MerchantBusinessQRCode.objects.get(sn=business_qrcode)
                    if instance.is_c_bind == 1:
                        inviter_id = instance.customeruservice_id
                        inviter_user = instance.customeruservice
                        while True:
                            inviter_user = CustomerUserVice.objects.get(id=inviter_id)
                            origin_point = inviter_user.point
                            origin_obtain_point = inviter_user.obtain_point
                            new_point = origin_point + Decimal('50')
                            new_obtain_point = origin_obtain_point + Decimal('50')
                            result = CustomerUserVice.objects.filter(id=inviter_id, point=origin_point).update(
                                point=new_point,
                                obtain_point=new_obtain_point
                            )
                            if result == 0:
                                continue
                            break
                        recoad_data = {}
                        recoad_data['customeruservice_id'] = inviter_user.id
                        recoad_data['operate_type'] = 1
                        recoad_data['point_type'] = 3
                        recoad_data['point'] = + Decimal('50')
                        recoad_data['point_balance'] = new_point
                        recoad_data['explain'] = '邀请新用户参与验卡'
                        get_point_record_db_model(inviter_user.id).objects.create(**recoad_data)
                        order_info.is_exchange = 1
                        order_info.save()
                        customeruservice = order_info.customeruservice
                        customeruservice.is_first = 0
                        customeruservice.inviter_id = inviter_id
                        customeruservice.inviter_time = order_info.success_time
                        customeruservice.save()
                        try:
                            result = CustomerUserViceInviterRecord.objects.filter(customeruservice_id=customeruservice.id, inviter_id=inviter_id).update(inviter_time=order_info.success_time, is_inviter=1)
                            if result == 0:
                                pass
                            else:
                                instance.inviter_count += 1
                                instance.save()
                                if inviter_user.wyf_gzh_openid:
                                    today = datetime.datetime.now()
                                    activity = CustomizeActivity.objects.filter(activity_type=9, available_begin_time__lte=today, available_end_time__gte=today).first()
                                    if activity:
                                        inviter_count = CustomerUserViceInviterRecord.objects.filter(inviter_id=inviter_id, inviter_time__gte=activity.available_begin_time, inviter_time__lte=activity.available_end_time).count()
                                        if inviter_count in [1, 2]:
                                            response_data = {
                                                "keyword1": {
                                                    "value": "已成功邀请1位用户",
                                                    "color": "#878787"
                                                },
                                                "keyword2": {
                                                    "value": "点击立即进入",
                                                    "color": "#878787"
                                                },
                                                "keyword3": {
                                                    "value": "微邮惠商圈",
                                                    "color": "#ed6f56"
                                                },
                                                "keyword4": {
                                                    "value": "查看邀请进度",
                                                    "color": "#ed6f56"
                                                },
                                                "keyword5": {
                                                    "value": "邀请任务完成即可参与抽奖！",
                                                    "color": "#ed6f56"
                                                }
                                            }
                                            invite_push_notification(inviter_user.wyf_gzh_openid, response_data)
                                        elif inviter_count == 3:
                                            response_data = {
                                                "keyword1": {
                                                    "value": "本周已成功邀请3位用户",
                                                    "color": "#878787"
                                                },
                                                "keyword2": {
                                                    "value": "立即点击参与抽奖！",
                                                    "color": "#878787"
                                                },
                                                "keyword3": {
                                                    "value": "微邮惠商圈",
                                                    "color": "#ed6f56"
                                                },
                                                "keyword4": {
                                                    "value": "点击立即进入",
                                                    "color": "#ed6f56"
                                                },
                                                "keyword5": {
                                                    "value": "赢取更多代金券福利！",
                                                    "color": "#ed6f56"
                                                }
                                            }
                                            invite_push_notification(inviter_user.wyf_gzh_openid, response_data)
                                if instance.inviter_count >= 3:
                                    cashactivitydata = instance.cashactivitydata
                                    if not cashactivitydata:
                                        companyuser = instance.user
                                        companyuser_id = companyuser.id
                                        date_now = datetime.datetime.now()
                                        nonce_str = random_str()
                                        cash_activity_id = 355
                                        amount = Decimal('5')
                                        cash_activity = CashActivity.objects.get(id=cash_activity_id)
                                        cashactivitydata_data = {}
                                        cashactivitydata_data['companyuser_id'] = companyuser_id
                                        cashactivitydata_data['cash_activity_id'] = cash_activity.id
                                        cashactivitydata_data['amount'] = amount
                                        cashactivitydata_data['create_time'] = date_now
                                        cashactivitydata_data['nonce_str'] = nonce_str
                                        cashactivitydata_data['desc'] = cash_activity.name
                                        cashactivitydata_data['mobile'] = companyuser.mobile
                                        cashactivitydata_data['openid'] = companyuser.openid
                                        cashactivitydata_data['appid'] = 'wxb67b94c00d7cd83f'
                                        cashactivitydata_data['partner_trade_no'] = f'{date_now.strftime("%Y%m%d%H%M%S")}{"%06d" % randint(0, 999999)}{"%06d" % companyuser_id}'
                                        cashactivitydata = CashActivityData.objects.create(**cashactivitydata_data)
                                        while True:
                                            activity = CashActivity.objects.get(id=cash_activity_id)
                                            activity_origin_take_amount = activity.take_amount
                                            activity_origin_remain_amount = activity.remain_amount
                                            activity_new_take_amount = activity_origin_take_amount + amount
                                            activity_new_remain_amount = activity_origin_remain_amount - amount
                                            result = CashActivity.objects.filter(
                                                take_amount=activity_origin_take_amount,
                                                remain_amount=activity_origin_remain_amount,
                                                id=cash_activity_id).update(
                                                take_amount=activity_new_take_amount,
                                                remain_amount=activity_new_remain_amount)
                                            if result == 0:
                                                continue
                                            break
                                        instance.cashactivitydata_id = cashactivitydata.id
                                        instance.is_achieve = 1
                                        instance.award_time = CustomerUserViceInviterRecord.objects.filter(qrcode_id=instance.id, is_inviter=1)[2].inviter_time
                                        instance.save()
                        except:
                            pass
            except:
                pass
    elif order_data['trade_state'] == 'PAYERROR':
        order_info.order_status = 3
        order_info.save()
    return '写入成功'


@shared_task
def dispose_customer_wxpay_order_result(data):
    ciphertext = data['resource']['ciphertext']
    nonce = data['resource']['nonce']
    associated_data = data['resource']['associated_data']
    key = 'xinxiangxinxiangxinxiangxinxiang'
    key_bytes = str.encode(key)
    nonce_bytes = str.encode(nonce)
    ad_bytes = str.encode(associated_data)
    ciphertext_bytes = base64.b64decode(ciphertext)
    aesgcm = AESGCM(key_bytes)
    order_data = json.loads(aesgcm.decrypt(nonce_bytes, ciphertext_bytes, ad_bytes).decode())
    try:
        order_info = CustomeruserBindCardOrder.objects.get(out_trade_no=order_data.pop('out_trade_no'))
    except Exception as e:
        logging.error(e)
        return '未能找到对应订单号订单'
    if order_data['trade_state'] == 'SUCCESS':
        order_info.success_time = order_data['success_time'].replace('T', ' ').replace('+08:00', '')
        order_info.trade_no = order_data['transaction_id']
        order_info.transaction_id = order_data['transaction_id']
        order_info.bank_type = order_data['bank_type']
        if order_data['bank_type'] == 'PSBC_DEBIT':
            order_info.bank_type_sign = 1
        elif order_data['bank_type'] == 'PSBC_CREDIT':
            order_info.bank_type_sign = 2
        else:
            order_info.bank_type_sign = 3
        order_info.order_status = 2
        order_info.save()
        data_json = {}
        data_json['out_trade_no'] = order_info.out_trade_no
        data_json['out_refund_no'] = order_info.out_refund_no
        data_json['notify_url'] = 'https://api.sutpay.com/cpay/customer_refund_result/'
        data_json['amount'] = {"refund": 1, "total": 1, "currency": "CNY"}
        data_dict = pay_transactions_refund(data_json)
        if 'code' in data_dict:
            order_info.message = data_dict['message']
            order_info.order_status = 5
            order_info.save()
        if 'PSBC' not in order_data['bank_type']:
            pass
        else:
            customeruser = order_info.customeruser
            customeruser.is_psbc_card = 1
            customeruser.save()
    elif order_data['trade_state'] == 'PAYERROR':
        order_info.order_status = 3
        order_info.save()
    return '写入成功'


@shared_task
def dispose_customervice_wxpay_order_result(data):
    ciphertext = data['resource']['ciphertext']
    nonce = data['resource']['nonce']
    associated_data = data['resource']['associated_data']
    key = 'xinxiangxinxiangxinxiangxinxiang'
    key_bytes = str.encode(key)
    nonce_bytes = str.encode(nonce)
    ad_bytes = str.encode(associated_data)
    ciphertext_bytes = base64.b64decode(ciphertext)
    aesgcm = AESGCM(key_bytes)
    order_data = json.loads(aesgcm.decrypt(nonce_bytes, ciphertext_bytes, ad_bytes).decode())
    try:
        order_info = BindCardOrder.objects.get(out_trade_no=order_data.pop('out_trade_no'))
    except Exception as e:
        logging.error(e)
        return '未能找到对应订单号订单'
    if order_data['trade_state'] == 'SUCCESS':
        order_info.success_time = order_data['success_time'].replace('T', ' ').replace('+08:00', '')
        order_info.trade_no = order_data['transaction_id']
        order_info.transaction_id = order_data['transaction_id']
        order_info.bank_type = order_data['bank_type']
        if order_data['bank_type'] == 'PSBC_DEBIT':
            order_info.bank_type_sign = 1
        elif order_data['bank_type'] == 'PSBC_CREDIT':
            order_info.bank_type_sign = 2
        else:
            order_info.bank_type_sign = 3
        order_info.order_status = 2
        order_info.prize_status = 6
        order_info.save()
        data_json = {}
        data_json['out_trade_no'] = order_info.out_trade_no
        data_json['out_refund_no'] = order_info.out_refund_no
        data_json['notify_url'] = 'https://api.sutpay.com/cpay/customervice_refund_result/'
        data_json['amount'] = {"refund": 1, "total": 1, "currency": "CNY"}
        data_dict = pay_transactions_refund(data_json)
        if 'code' in data_dict:
            order_info.message = data_dict['message']
            order_info.order_status = 5
            order_info.save()
        if 'PSBC' not in order_data['bank_type']:
            pass
        else:
            customeruservice = order_info.customeruservice
            customeruservice.is_psbc_card = 1
            customeruservice.save()
    elif order_data['trade_state'] == 'PAYERROR':
        order_info.order_status = 3
        order_info.save()
    return '写入成功'


@shared_task
def dispose_merchant_wxpay_order_result(data):
    ciphertext = data['resource']['ciphertext']
    nonce = data['resource']['nonce']
    associated_data = data['resource']['associated_data']
    key = 'xinxiangxinxiangxinxiangxinxiang'
    key_bytes = str.encode(key)
    nonce_bytes = str.encode(nonce)
    ad_bytes = str.encode(associated_data)
    ciphertext_bytes = base64.b64decode(ciphertext)
    aesgcm = AESGCM(key_bytes)
    order_data = json.loads(aesgcm.decrypt(nonce_bytes, ciphertext_bytes, ad_bytes).decode())
    try:
        order_info = MerchantBindCardOrder.objects.get(out_trade_no=order_data.pop('out_trade_no'))
    except Exception as e:
        logging.error(e)
        return '未能找到对应订单号订单'
    if order_data['trade_state'] == 'SUCCESS':
        order_info.success_time = order_data['success_time'].replace('T', ' ').replace('+08:00', '')
        order_info.trade_no = order_data['transaction_id']
        order_info.transaction_id = order_data['transaction_id']
        order_info.bank_type = order_data['bank_type']
        if order_data['bank_type'] == 'PSBC_DEBIT':
            order_info.bank_type_sign = 1
        elif order_data['bank_type'] == 'PSBC_CREDIT':
            order_info.bank_type_sign = 2
        else:
            order_info.bank_type_sign = 3
        order_info.order_status = 2
        order_info.save()
        data_json = {}
        data_json['out_trade_no'] = order_info.out_trade_no
        data_json['out_refund_no'] = order_info.out_refund_no
        data_json['notify_url'] = 'https://api.sutpay.com/cpay/merchant_refund_result/'
        data_json['amount'] = {"refund": 1, "total": 1, "currency": "CNY"}
        data_dict = pay_transactions_refund(data_json)
        if 'code' in data_dict:
            order_info.message = data_dict['message']
            order_info.order_status = 5
            order_info.save()
        if 'PSBC' in order_data['bank_type']:
            merchantuser = order_info.merchantuser
            merchantuser.is_psbc_card = 1
            merchantuser.save()
    elif order_data['trade_state'] == 'PAYERROR':
        order_info.order_status = 3
        order_info.save()
    return '写入成功'


@shared_task
def dispose_refund_order_result(data):
    ciphertext = data['resource']['ciphertext']
    nonce = data['resource']['nonce']
    associated_data = data['resource']['associated_data']
    key = 'xinxiangxinxiangxinxiangxinxiang'
    key_bytes = str.encode(key)
    nonce_bytes = str.encode(nonce)
    ad_bytes = str.encode(associated_data)
    ciphertext_bytes = base64.b64decode(ciphertext)
    aesgcm = AESGCM(key_bytes)
    order_data = json.loads(aesgcm.decrypt(nonce_bytes, ciphertext_bytes, ad_bytes).decode())
    try:
        order_info = BindCardOrder.objects.get(out_trade_no=order_data.pop('out_trade_no'))
    except Exception as e:
        logging.error(e)
        return '未能找到对应订单号订单'
    if order_data['refund_status'] == 'SUCCESS':
        order_info.refund_time = order_data['success_time'].replace('T', ' ').replace('+08:00', '')
        order_info.refund_id = order_data['refund_id']
        order_info.user_received_account = order_data['user_received_account']
        order_info.order_status = 4
        order_info.save()
    else:
        order_info.order_status = 5
        order_info.save()
    return '写入成功'


@shared_task
def dispose_customer_refund_order_result(data):
    ciphertext = data['resource']['ciphertext']
    nonce = data['resource']['nonce']
    associated_data = data['resource']['associated_data']
    key = 'xinxiangxinxiangxinxiangxinxiang'
    key_bytes = str.encode(key)
    nonce_bytes = str.encode(nonce)
    ad_bytes = str.encode(associated_data)
    ciphertext_bytes = base64.b64decode(ciphertext)
    aesgcm = AESGCM(key_bytes)
    order_data = json.loads(aesgcm.decrypt(nonce_bytes, ciphertext_bytes, ad_bytes).decode())
    try:
        order_info = CustomeruserBindCardOrder.objects.get(out_trade_no=order_data.pop('out_trade_no'))
    except Exception as e:
        logging.error(e)
        return '未能找到对应订单号订单'
    if order_data['refund_status'] == 'SUCCESS':
        order_info.refund_time = order_data['success_time'].replace('T', ' ').replace('+08:00', '')
        order_info.refund_id = order_data['refund_id']
        order_info.user_received_account = order_data['user_received_account']
        order_info.order_status = 4
        order_info.save()
    else:
        order_info.order_status = 5
        order_info.save()
    return '写入成功'


@shared_task
def dispose_customervice_refund_order_result(data):
    ciphertext = data['resource']['ciphertext']
    nonce = data['resource']['nonce']
    associated_data = data['resource']['associated_data']
    key = 'xinxiangxinxiangxinxiangxinxiang'
    key_bytes = str.encode(key)
    nonce_bytes = str.encode(nonce)
    ad_bytes = str.encode(associated_data)
    ciphertext_bytes = base64.b64decode(ciphertext)
    aesgcm = AESGCM(key_bytes)
    order_data = json.loads(aesgcm.decrypt(nonce_bytes, ciphertext_bytes, ad_bytes).decode())
    try:
        order_info = BindCardOrder.objects.get(out_trade_no=order_data.pop('out_trade_no'))
    except Exception as e:
        logging.error(e)
        return '未能找到对应订单号订单'
    if order_data['refund_status'] == 'SUCCESS':
        order_info.refund_time = order_data['success_time'].replace('T', ' ').replace('+08:00', '')
        order_info.refund_id = order_data['refund_id']
        order_info.user_received_account = order_data['user_received_account']
        order_info.order_status = 4
        order_info.save()
    else:
        order_info.order_status = 5
        order_info.save()
    return '写入成功'


@shared_task
def dispose_merchant_refund_order_result(data):
    ciphertext = data['resource']['ciphertext']
    nonce = data['resource']['nonce']
    associated_data = data['resource']['associated_data']
    key = 'xinxiangxinxiangxinxiangxinxiang'
    key_bytes = str.encode(key)
    nonce_bytes = str.encode(nonce)
    ad_bytes = str.encode(associated_data)
    ciphertext_bytes = base64.b64decode(ciphertext)
    aesgcm = AESGCM(key_bytes)
    order_data = json.loads(aesgcm.decrypt(nonce_bytes, ciphertext_bytes, ad_bytes).decode())
    try:
        order_info = MerchantBindCardOrder.objects.get(out_trade_no=order_data.pop('out_trade_no'))
    except Exception as e:
        logging.error(e)
        return '未能找到对应订单号订单'
    if order_data['refund_status'] == 'SUCCESS':
        order_info.refund_time = order_data['success_time'].replace('T', ' ').replace('+08:00', '')
        order_info.refund_id = order_data['refund_id']
        order_info.user_received_account = order_data['user_received_account']
        order_info.order_status = 4
        order_info.save()
    else:
        order_info.order_status = 5
        order_info.save()
    return '写入成功'


@shared_task
def submit_shop_can_subsidy(data):
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.connect(("8.8.8.8", 80))
    ip = s.getsockname()[0]
    if ip == '172.16.0.14':
        submit_shop_can_subsidy.delay(data)
        return '移交其他服务器处理'
    merchant_id = data['merchant_id']
    a = int(data['a'])
    if a >= 30:
        return '开通超时'
    merchant = Merchant.objects.get(id=merchant_id)
    try:
        subsidy_result = check_shop_can_subsidy(merchant.ruiyinxinMerchantID, 'sxf_tq')
        logger.info(str(subsidy_result))
        if subsidy_result['is_can_subsidy'] == 1:
            merchant.is_sxf_subsidy = 1
            merchant.save()
            return '开通随行付权限成功'
        elif subsidy_result['is_can_subsidy'] == 0:
            check_shop_can_subsidy(merchant.ruiyinxinMerchantID, 'ruiyinxin')
            merchant.subsidy_channel_id = 2
            merchant.is_ryx_subsidy = 1
            merchant.save()
            return '开通瑞银信权限成功'
        elif subsidy_result['is_can_subsidy'] == 2:
            time.sleep(10)
            submit_shop_can_subsidy.delay({"merchant_id": merchant_id, 'a': a+1})
    except:
        submit_shop_can_subsidy.delay({"merchant_id": merchant_id, 'a': a+1})
    return '开通补贴权限结束'


@shared_task
def get_sy_orderinfo(data):
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.connect(("8.8.8.8", 80))
    ip = s.getsockname()[0]
    if ip == '172.16.0.14':
        get_sy_orderinfo.delay(data)
        return '移交其他服务器处理'
    order_sy_id = data['id']
    order_sy = SYOrderInfo.objects.get(id=order_sy_id)
    shop_order_id = order_sy.shop_order_id
    shop_id = order_sy.shop_id
    merchant = Merchant.objects.filter(ruiyinxinMerchantID=shop_id).first()
    if not merchant:
        url = 'https://manage.gdwxyf.com/internal/searchmerchantbyshopids.do'
        body = {
            'account': 'gdyouzheng',
            'shopId': shop_id
        }
        reaponse = requests.post(url=url, data=body)
        try:
            reaponse = reaponse.json()
        except:
            order_sy.order_status = 4
            order_sy.save()
            return '获取商户信息失败'
        if 'wx_channel_merchant_id' not in reaponse:
            order_sy.order_status = 4
            order_sy.save()
            return '获取商户信息失败'
        with transaction.atomic():
            # 创建事务保存点
            save_id = transaction.savepoint()
            try:
                merchant_data = {}
                channel_mch_id = reaponse['channel_mch_id']
                channel = Channel.objects.filter(mchid=channel_mch_id).first()
                if channel:
                    merchant_data['channel_id'] = channel.id
                business_no = reaponse['trueid']
                merchant_data['name'] = reaponse['shopName']
                merchant_data['short_name'] = reaponse['shopNickname']
                merchant_data['wyfMerchantID'] = business_no
                merchant_data['ruiyinxinMerchantID'] = shop_id
                merchant_data['create_time'] = datetime.datetime.now()
                if reaponse['businessType'] == '1':
                    merchant_data['subject_type'] = 3
                elif reaponse['businessType'] == '2':
                    merchant_data['subject_type'] = 2
                elif reaponse['businessType'] == '3':
                    merchant_data['subject_type'] = 1
                else:
                    merchant_data['subject_type'] = 4
                level = Level.objects.filter(acceptId=reaponse['accept_id']).first()
                if level:
                    merchant_data['level_id'] = level.id
                merchantuser, create = MerchantUser.objects.get_or_create(mobile=reaponse['phone'], defaults={"mobile": reaponse['phone']})
                merchant_data['merchantuser_id'] = merchantuser.id
                merchant_data['account_name'] = reaponse['cardName']
                merchant_data['account_bank'] = reaponse['bankName']
                merchant_data['account_number'] = reaponse['card']
                if 'latitude' in reaponse:
                    merchant_data['latitude'] = reaponse['latitude']
                if 'longitude' in reaponse:
                    merchant_data['longitude'] = reaponse['longitude']
                card_copy = f'youhuiquanyi/{business_no}/card_copy.png'
                card_copy_url = sync_img_to_local(f'http://ossmerchant.gdwxyf.com/{shop_id}/cardFace.png', key=card_copy)
                if card_copy_url:
                    merchant_data['card_copy'] = card_copy
                merchant_data['identification_number'] = reaponse['identity']
                merchant_data['store_address'] = reaponse['address']
                store_header_copy = f'youhuiquanyi/{business_no}/store_header_copy.png'
                store_header_copy_url = sync_img_to_local(f'http://ossmerchant.gdwxyf.com/{shop_id}/merchantHead.png', key=store_header_copy)
                if store_header_copy_url:
                    merchant_data['store_header_copy'] = store_header_copy
                merchant, create = Merchant.objects.update_or_create(defaults=merchant_data, wyfMerchantID=str(business_no))
                if not merchant.managerName:
                    merchant.managerName = reaponse['keeper']
                if not merchant.managermobile:
                    merchant.managermobile = reaponse['phone']
                if not merchant.prov_name:
                    merchant.prov_name = reaponse['province']
                    merchant.city_name = reaponse['city']
                    merchant.district_name = reaponse['area']
                if not merchant.business_category:
                    merchant.business_category_id = 1
                if not merchant.store_logo_copy:
                    merchant.store_logo_copy = 'default_merchant_logo.jpg'
                merchant.save()
                if create and merchant.channel:
                    store_header_copy = f'youhuiquanyi/{business_no}/merchant_image{"%06d" % randint(0, 999999)}.png'
                    store_header_copy_url = sync_img_to_local(f'http://ossmerchant.gdwxyf.com/{shop_id}/merchantHead.png', key=store_header_copy)
                    if store_header_copy_url:
                        MerchantImage.objects.create(merchant_id=merchant.id, image=store_header_copy)
                    store_indoor_copy = f'youhuiquanyi/{business_no}/merchant_image{"%06d" % randint(0, 999999)}.png'
                    store_indoor_copy_url = sync_img_to_local(f'http://ossmerchant.gdwxyf.com/{shop_id}/otherPhoto3.png', key=store_indoor_copy)
                    if store_indoor_copy_url:
                        MerchantImage.objects.create(merchant_id=merchant.id, image=store_indoor_copy)
                    merchantCheck = f'youhuiquanyi/{business_no}/merchant_image{"%06d" % randint(0, 999999)}.png'
                    merchantCheck_url = sync_img_to_local(f'http://ossmerchant.gdwxyf.com/{shop_id}/merchantCheck.png', key=merchantCheck)
                    if merchantCheck_url:
                        MerchantImage.objects.create(merchant_id=merchant.id, image=merchantCheck)
                merchantchannel_data = {
                    'merchant_id': merchant.id
                }
                if reaponse['allchannelJson']['channel_ruiyinxin']:
                    if reaponse['allchannelJson']['channel_ruiyinxin']['status'] == 1:
                        merchantchannel_data['smid'] = reaponse['allchannelJson']['channel_ruiyinxin']['merchant_other_4']
                        merchantchannel_data['channel_mchid'] = reaponse['allchannelJson']['channel_ruiyinxin']['merchant_other_7']
                        merchantchannel_data['channel_id'] = 2
                        merchantchannel_data['sub_mchid'] = reaponse['allchannelJson']['channel_ruiyinxin']['merchant_other_3']
                        MerchantChannel.objects.get_or_create(defaults=merchantchannel_data, merchant_id=merchant.id, channel_id=merchantchannel_data['channel_id'])
                if reaponse['allchannelJson']['channel_sxf_tq']:
                    if reaponse['allchannelJson']['channel_sxf_tq']['status'] == 1:
                        merchantchannel_data['smid'] = reaponse['allchannelJson']['channel_sxf_tq']['merchant_other_4'] if 'merchant_other_4' in reaponse['allchannelJson']['channel_sxf_tq'] else ''
                        merchantchannel_data['channel_mchid'] = reaponse['allchannelJson']['channel_sxf_tq']['merchant_no']
                        merchantchannel_data['channel_id'] = 3
                        merchantchannel_data['sub_mchid'] = reaponse['allchannelJson']['channel_sxf_tq']['merchant_other_3'] if 'merchant_other_3' in reaponse['allchannelJson']['channel_sxf_tq'] else ''
                        MerchantChannel.objects.get_or_create(defaults=merchantchannel_data, merchant_id=merchant.id, channel_id=merchantchannel_data['channel_id'])
                transaction.savepoint_commit(save_id)
            except Exception as e:
                print(e)
                order_sy.order_status = 4
                order_sy.save()
                return '获取商户信息失败'
    try:
        if merchant.subsidy_channel_id == 3 and merchant.is_sxf_subsidy == 0:
            submit_shop_can_subsidy.delay({"merchant_id": merchant.id, 'a': 0})
    except Exception as e:
        logger.info(str(e))
        pass
    business_no = merchant.wyfMerchantID
    order_result = pull_orderinfo(order_sy.shop_order_id)
    if order_result:
        order_data = {}
        order_result = order_result[0]
        order_data['orderNum'] = order_result['orderNum']
        order_data['order_id'] = order_result['orderNum']
        order_data['create_time'] = order_result['createdAt'].replace('T', ' ').replace('+08:00', '')
        order_data['state'] = 3
        order_data['platform'] = 1
        goods_id = int(order_result['goodsId']) + 10000000
        try:
            launchedgoods = LaunchedGoods.objects.filter(goods_id=goods_id).first()
            if not launchedgoods:
                launchedgoods = pull_goods(order_result['goodsId'], business_no)
            rimerchant = launchedgoods.goods.merchant
            if rimerchant:
                order_data['merchant_id'] = merchant.id
        except Exception as e:
            order_sy.order_status = 4
            order_sy.save()
            return '同步商品失败'
        try:
            # mobile_dic = {
            #     "vm444061111269466112": "13922779795"
            # }
            # userMobile = pull_userMobile(order_sy.shop_order_id)
            # userMobile = mobile_dic[shop_order_id]
            # riuser = CustomerUser.objects.filter(mobile=userMobile).first()
            # if riuser:
            #     order_data['customer_user_id'] = riuser.id
            # else:
            #     riuser = pull_user(userMobile)
            #     riuser = CustomerUser.objects.get(id=riuser.id)
            #     order_data['customer_user_id'] = riuser.id
            pass
        except Exception as e:
            pass
            # order_sy.order_status = 4
            # order_sy.save()
            # return '同步C端用户失败'
        ORDER_USER_TIME = {
            "vm444054287560482816": "2023-08-01 09:05:37",
            "vm444062238018904064": "2023-08-01 09:05:49",
            "vm444062284374351872": "2023-08-01 09:05:59",
            "vm444061229053911040": "2023-08-01 09:05:51",
            "vm444062413969956864": "2023-08-01 09:06:11",
            "vm444062258860400640": "2023-08-01 09:09:12",
            "vm444058350867980288": "2023-08-01 09:25:45",
            "vm444085134296621056": "2023-08-01 10:49:08",
            "vm444087959344914432": "2023-08-01 10:49:29",
            "vm443863223201439744": "2023-08-01 10:49:45",
            "vm444087942043410432": "2023-08-01 10:49:50",
            "vm444087657506021376": "2023-08-01 10:50:17",
            "vm444078328233074688": "2023-08-01 10:50:48",
            "vm444095137191567360": "2023-08-01 18:00:40",
        }
        order_data['total_point'] = Decimal(order_result['allLocalPoint'])
        order_data['total_price'] = Decimal(order_result['allLocalPoint']) / 100
        order_data['payable_settled_amount'] = Decimal(order_result['allLocalPoint']) / 100
        order_data['total_count'] = order_result['goodsNum']
        order_data['payment_time'] = order_result['createdAt'].replace('T', ' ').replace('+08:00', '')
        if order_result['writeoffAt']:
            writeoffAt = order_result['writeoffAt'].replace('T', ' ').replace('+08:00', '')
        else:
            writeoffAt = order_sy.create_time
        order_data['use_time'] = writeoffAt
        order_data['level_id'] = launchedgoods.level_id
        order_data['state'] = 3
        order_data['settlement_state'] = 1
        try:
            instance, create = OrderInfo.objects.get_or_create(orderNum=order_data['orderNum'], defaults=order_data)
            if create:
                add_orderinfo_index.delay({'id': instance.id})
                print(instance.id)
                ordergoods = OrderGoods.objects.create(
                    order=instance,
                    launched_goods=launchedgoods,
                    count=order_result['goodsNum'],
                    price=launchedgoods.goods.price,
                    point=launchedgoods.goods.point,
                    goods_id=launchedgoods.goods_id,
                    merchant_id=launchedgoods.goods.merchant_id
                )
                # coupons_id = '%06d' % randint(0, 999999) + '%06d' % randint(0, 999999) + '%04d' % randint(0, 999999)
                # GoodsCoupons.objects.create(
                #     coupons_id=coupons_id,
                #     status=2,
                #     goods_id=ordergoods.goods_id,
                #     order_id=instance.id,
                #     validity_time=datetime.datetime.now(),
                #     use_time=writeoffAt
                # )
            order_sy.order_status = 3
            order_sy.merchant_id = merchant.id
            order_sy.level_id = merchant.level_id
            order_sy.order_id = instance.id
            order_sy.save()
        except Exception as e:
            print(e)
            print(order_sy_id)
            print('失败')
    else:
        order_sy.order_status = 4
        order_sy.save()
        print(shop_order_id)
        return '找不到订单信息'


@shared_task
def get_sy_orderinfo1(data):
    # s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    # s.connect(("8.8.8.8", 80))
    # ip = s.getsockname()[0]
    # if ip == '172.16.0.14':
    #     get_sy_orderinfo.delay(data)
    #     return '移交其他服务器处理'
    order_sy_id = data['id']
    order_sy = SYOrderInfo.objects.get(id=order_sy_id)
    shop_order_id = order_sy.shop_order_id
    shop_id = order_sy.shop_id
    merchant = Merchant.objects.filter(ruiyinxinMerchantID=shop_id).first()
    if not merchant:
        url = 'https://manage.gdwxyf.com/internal/searchmerchantbyshopids.do'
        body = {
            'account': 'gdyouzheng',
            'shopId': shop_id
        }
        reaponse = requests.post(url=url, data=body)
        try:
            reaponse = reaponse.json()
        except:
            order_sy.order_status = 4
            order_sy.save()
            return '获取商户信息失败'
        if 'wx_channel_merchant_id' not in reaponse:
            order_sy.order_status = 4
            order_sy.save()
            return '获取商户信息失败'
        with transaction.atomic():
            # 创建事务保存点
            save_id = transaction.savepoint()
            try:
                merchant_data = {}
                channel_mch_id = reaponse['channel_mch_id']
                channel = Channel.objects.filter(mchid=channel_mch_id).first()
                if channel:
                    merchant_data['channel_id'] = channel.id
                business_no = reaponse['trueid']
                merchant_data['name'] = reaponse['shopName']
                merchant_data['short_name'] = reaponse['shopNickname']
                merchant_data['wyfMerchantID'] = business_no
                merchant_data['ruiyinxinMerchantID'] = shop_id
                merchant_data['create_time'] = datetime.datetime.now()
                if reaponse['businessType'] == '1':
                    merchant_data['subject_type'] = 3
                elif reaponse['businessType'] == '2':
                    merchant_data['subject_type'] = 2
                elif reaponse['businessType'] == '3':
                    merchant_data['subject_type'] = 1
                else:
                    merchant_data['subject_type'] = 4
                level = Level.objects.filter(acceptId=reaponse['accept_id']).first()
                if level:
                    merchant_data['level_id'] = level.id
                merchantuser, create = MerchantUser.objects.get_or_create(mobile=reaponse['phone'], defaults={"mobile": reaponse['phone']})
                merchant_data['merchantuser_id'] = merchantuser.id
                merchant_data['account_name'] = reaponse['cardName']
                merchant_data['account_bank'] = reaponse['bankName']
                merchant_data['account_number'] = reaponse['card']
                if 'latitude' in reaponse:
                    merchant_data['latitude'] = reaponse['latitude']
                if 'longitude' in reaponse:
                    merchant_data['longitude'] = reaponse['longitude']
                card_copy = f'youhuiquanyi/{business_no}/card_copy.png'
                card_copy_url = sync_img_to_local(f'http://ossmerchant.gdwxyf.com/{shop_id}/cardFace.png', key=card_copy)
                if card_copy_url:
                    merchant_data['card_copy'] = card_copy
                merchant_data['identification_number'] = reaponse['identity']
                merchant_data['store_address'] = reaponse['address']
                store_header_copy = f'youhuiquanyi/{business_no}/store_header_copy.png'
                store_header_copy_url = sync_img_to_local(f'http://ossmerchant.gdwxyf.com/{shop_id}/merchantHead.png', key=store_header_copy)
                if store_header_copy_url:
                    merchant_data['store_header_copy'] = store_header_copy
                merchant, create = Merchant.objects.update_or_create(defaults=merchant_data, wyfMerchantID=business_no)
                if not merchant.managerName:
                    merchant.managerName = reaponse['keeper']
                if not merchant.managermobile:
                    merchant.managermobile = reaponse['phone']
                if not merchant.prov_name:
                    merchant.prov_name = reaponse['province']
                    merchant.city_name = reaponse['city']
                    merchant.district_name = reaponse['area']
                if not merchant.business_category:
                    merchant.business_category_id = 1
                if not merchant.store_logo_copy:
                    merchant.store_logo_copy = 'default_merchant_logo.jpg'
                merchant.save()
                if create and merchant.channel:
                    store_header_copy = f'youhuiquanyi/{business_no}/merchant_image{"%06d" % randint(0, 999999)}.png'
                    store_header_copy_url = sync_img_to_local(f'http://ossmerchant.gdwxyf.com/{shop_id}/merchantHead.png', key=store_header_copy)
                    if store_header_copy_url:
                        MerchantImage.objects.create(merchant_id=merchant.id, image=store_header_copy)
                    store_indoor_copy = f'youhuiquanyi/{business_no}/merchant_image{"%06d" % randint(0, 999999)}.png'
                    store_indoor_copy_url = sync_img_to_local(f'http://ossmerchant.gdwxyf.com/{shop_id}/otherPhoto3.png', key=store_indoor_copy)
                    if store_indoor_copy_url:
                        MerchantImage.objects.create(merchant_id=merchant.id, image=store_indoor_copy)
                    merchantCheck = f'youhuiquanyi/{business_no}/merchant_image{"%06d" % randint(0, 999999)}.png'
                    merchantCheck_url = sync_img_to_local(f'http://ossmerchant.gdwxyf.com/{shop_id}/merchantCheck.png', key=merchantCheck)
                    if merchantCheck_url:
                        MerchantImage.objects.create(merchant_id=merchant.id, image=merchantCheck)
                merchantchannel_data = {
                    'merchant_id': merchant.id
                }
                if reaponse['allchannelJson']['channel_ruiyinxin']:
                    if reaponse['allchannelJson']['channel_ruiyinxin']['status'] == 1:
                        merchantchannel_data['smid'] = reaponse['allchannelJson']['channel_ruiyinxin']['merchant_other_4']
                        merchantchannel_data['channel_mchid'] = reaponse['allchannelJson']['channel_ruiyinxin']['merchant_other_7']
                        merchantchannel_data['channel_id'] = 2
                        merchantchannel_data['sub_mchid'] = reaponse['allchannelJson']['channel_ruiyinxin']['merchant_other_3']
                        MerchantChannel.objects.get_or_create(defaults=merchantchannel_data, merchant_id=merchant.id, channel_id=merchantchannel_data['channel_id'])
                if reaponse['allchannelJson']['channel_sxf_tq']:
                    if reaponse['allchannelJson']['channel_sxf_tq']['status'] == 1:
                        merchantchannel_data['smid'] = reaponse['allchannelJson']['channel_sxf_tq']['merchant_other_4'] if 'merchant_other_4' in reaponse['allchannelJson']['channel_sxf_tq'] else ''
                        merchantchannel_data['channel_mchid'] = reaponse['allchannelJson']['channel_sxf_tq']['merchant_no']
                        merchantchannel_data['channel_id'] = 3
                        merchantchannel_data['sub_mchid'] = reaponse['allchannelJson']['channel_sxf_tq']['merchant_other_3'] if 'merchant_other_3' in reaponse['allchannelJson']['channel_sxf_tq'] else ''
                        MerchantChannel.objects.get_or_create(defaults=merchantchannel_data, merchant_id=merchant.id, channel_id=merchantchannel_data['channel_id'])
                transaction.savepoint_commit(save_id)
            except Exception as e:
                print(e)
                order_sy.order_status = 4
                order_sy.save()
                return '获取商户信息失败'
    try:
        if merchant.subsidy_channel_id == 3 and merchant.is_sxf_subsidy == 0:
            submit_shop_can_subsidy.delay({"merchant_id": merchant.id, 'a': 0})
    except Exception as e:
        logger.info(str(e))
        pass
    business_no = merchant.wyfMerchantID
    order_result = pull_orderinfo1(order_sy.shop_order_id)
    if order_result:
        order_data = {}
        order_result = order_result[0]
        order_data['orderNum'] = order_result['orderNum']
        order_data['order_id'] = order_result['orderNum']
        order_data['create_time'] = order_result['createdAt'].replace('T', ' ').replace('+08:00', '')
        order_data['state'] = 3
        order_data['platform'] = 1
        goods_id = int(order_result['equityId']) + 10000000
        try:
            launchedgoods = LaunchedGoods.objects.filter(goods_id=goods_id).first()
            if not launchedgoods:
                launchedgoods = pull_goods(order_result['equityId'], business_no)
            rimerchant = launchedgoods.goods.merchant
            if rimerchant:
                order_data['merchant_id'] = merchant.id
        except Exception as e:
            order_sy.order_status = 4
            order_sy.save()
            return '同步商品失败'
        try:
            # mobile_dic = {
            #     "vm444061111269466112": "13922779795"
            # }
            userMobile = order_result['userMobile']
            # userMobile = mobile_dic[shop_order_id]
            riuser = CustomerUser.objects.filter(mobile=userMobile).first()
            if riuser:
                order_data['customer_user_id'] = riuser.id
            else:
                riuser = pull_user(userMobile)
                riuser = CustomerUser.objects.get(id=riuser.id)
                order_data['customer_user_id'] = riuser.id
        except Exception as e:
            pass
            # order_sy.order_status = 4
            # order_sy.save()
            # return '同步C端用户失败'
        ORDER_USER_TIME = {
            "vm444054287560482816": "2023-08-01 09:05:37",
            "vm444062238018904064": "2023-08-01 09:05:49",
            "vm444062284374351872": "2023-08-01 09:05:59",
            "vm444061229053911040": "2023-08-01 09:05:51",
            "vm444062413969956864": "2023-08-01 09:06:11",
            "vm444062258860400640": "2023-08-01 09:09:12",
            "vm444058350867980288": "2023-08-01 09:25:45",
            "vm444085134296621056": "2023-08-01 10:49:08",
            "vm444087959344914432": "2023-08-01 10:49:29",
            "vm443863223201439744": "2023-08-01 10:49:45",
            "vm444087942043410432": "2023-08-01 10:49:50",
            "vm444087657506021376": "2023-08-01 10:50:17",
            "vm444078328233074688": "2023-08-01 10:50:48",
            "vm444095137191567360": "2023-08-01 18:00:40",
        }
        order_data['total_point'] = Decimal(order_result['usePoint'])
        order_data['total_price'] = Decimal(order_result['usePoint']) / 100
        order_data['payable_settled_amount'] = Decimal(order_result['usePoint']) / 100
        order_data['total_count'] = order_result['num']
        order_data['payment_time'] = order_result['createdAt'].replace('T', ' ').replace('+08:00', '')
        if order_result['successAt']:
            writeoffAt = order_result['successAt'].replace('T', ' ').replace('+08:00', '')
        else:
            writeoffAt = order_sy.create_time
        order_data['use_time'] = writeoffAt
        order_data['level_id'] = launchedgoods.level_id
        order_data['state'] = 3
        order_data['settlement_state'] = 1
        try:
            instance, create = OrderInfo.objects.get_or_create(orderNum=order_data['orderNum'], defaults=order_data)
            if create:
                ordergoods = OrderGoods.objects.create(
                    order=instance,
                    launched_goods=launchedgoods,
                    count=order_result['num'],
                    price=launchedgoods.goods.price,
                    point=launchedgoods.goods.point,
                    goods_id=launchedgoods.goods_id,
                    merchant_id=launchedgoods.goods.merchant_id
                )
                add_orderinfo_index.delay({'id': instance.id})
                print(instance.id)
                # coupons_id = '%06d' % randint(0, 999999) + '%06d' % randint(0, 999999) + '%04d' % randint(0, 999999)
                # GoodsCoupons.objects.create(
                #     coupons_id=coupons_id,
                #     status=2,
                #     goods_id=ordergoods.goods_id,
                #     order_id=instance.id,
                #     validity_time=datetime.datetime.now(),
                #     use_time=writeoffAt
                # )
            order_sy.order_status = 3
            order_sy.merchant_id = merchant.id
            order_sy.level_id = merchant.level_id
            order_sy.order_id = instance.id
            order_sy.save()
        except Exception as e:
            print(e)
            print(order_sy_id)
            print('失败')
    else:
        order_sy.order_status = 4
        order_sy.save()
        print(shop_order_id)
        return '找不到订单信息'


@shared_task
def get_residentialhostel_orderinfo(data):
    order_id = data['id']
    order = ResidentialHostelOrderInfo.objects.get(id=order_id)
    shop_order_id = order.shop_order_id
    shop_id = order.shop_id
    buyer_mobile = order.buyer_mobile
    buyer_name = order.buyer_name
    merchant = Merchant.objects.filter(ruiyinxinMerchantID=shop_id).first()
    if not merchant:
        url = 'https://manage.gdwxyf.com/internal/searchmerchantbyshopids.do'
        body = {
            'account': 'gdyouzheng',
            'shopId': shop_id
        }
        reaponse = requests.post(url=url, data=body)
        try:
            reaponse = reaponse.json()
        except:
            order.order_status = 4
            order.save()
            return '获取商户信息失败'
        if 'wx_channel_merchant_id' not in reaponse:
            order.order_status = 4
            order.save()
            return '获取商户信息失败'
        with transaction.atomic():
            # 创建事务保存点
            save_id = transaction.savepoint()
            try:
                merchant_data = {}
                channel_mch_id = reaponse['channel_mch_id']
                channel = Channel.objects.filter(mchid=channel_mch_id).first()
                if channel:
                    merchant_data['channel_id'] = channel.id
                business_no = reaponse['trueid']
                merchant_data['name'] = reaponse['shopName']
                merchant_data['short_name'] = reaponse['shopNickname']
                merchant_data['wyfMerchantID'] = business_no
                merchant_data['ruiyinxinMerchantID'] = shop_id
                merchant_data['create_time'] = datetime.datetime.now()
                if reaponse['businessType'] == '1':
                    merchant_data['subject_type'] = 3
                elif reaponse['businessType'] == '2':
                    merchant_data['subject_type'] = 2
                elif reaponse['businessType'] == '3':
                    merchant_data['subject_type'] = 1
                else:
                    merchant_data['subject_type'] = 4
                level = Level.objects.filter(acceptId=reaponse['accept_id']).first()
                if level:
                    merchant_data['level_id'] = level.id
                merchantuser, create = MerchantUser.objects.get_or_create(mobile=reaponse['phone'], defaults={"mobile": reaponse['phone']})
                merchant_data['merchantuser_id'] = merchantuser.id
                merchant_data['account_name'] = reaponse['cardName']
                merchant_data['account_bank'] = reaponse['bankName']
                merchant_data['account_number'] = reaponse['card']
                if 'latitude' in reaponse:
                    merchant_data['latitude'] = reaponse['latitude']
                if 'longitude' in reaponse:
                    merchant_data['longitude'] = reaponse['longitude']
                card_copy = f'youhuiquanyi/{business_no}/card_copy.png'
                card_copy_url = sync_img_to_local(f'http://ossmerchant.gdwxyf.com/{shop_id}/cardFace.png', key=card_copy)
                if card_copy_url:
                    merchant_data['card_copy'] = card_copy
                merchant_data['identification_number'] = reaponse['identity']
                merchant_data['store_address'] = reaponse['address']
                store_header_copy = f'youhuiquanyi/{business_no}/store_header_copy.png'
                store_header_copy_url = sync_img_to_local(f'http://ossmerchant.gdwxyf.com/{shop_id}/merchantHead.png', key=store_header_copy)
                if store_header_copy_url:
                    merchant_data['store_header_copy'] = store_header_copy
                merchant, create = Merchant.objects.update_or_create(defaults=merchant_data, wyfMerchantID=business_no)
                if not merchant.managerName:
                    merchant.managerName = reaponse['keeper']
                if not merchant.managermobile:
                    merchant.managermobile = reaponse['phone']
                if not merchant.prov_name:
                    merchant.prov_name = reaponse['province']
                    merchant.city_name = reaponse['city']
                    merchant.district_name = reaponse['area']
                if not merchant.business_category:
                    merchant.business_category_id = 1
                if not merchant.store_logo_copy:
                    merchant.store_logo_copy = 'default_merchant_logo.jpg'
                merchant.save()
                if create and merchant.channel:
                    store_header_copy = f'youhuiquanyi/{business_no}/merchant_image{"%06d" % randint(0, 999999)}.png'
                    store_header_copy_url = sync_img_to_local(f'http://ossmerchant.gdwxyf.com/{shop_id}/merchantHead.png', key=store_header_copy)
                    if store_header_copy_url:
                        MerchantImage.objects.create(merchant_id=merchant.id, image=store_header_copy)
                    store_indoor_copy = f'youhuiquanyi/{business_no}/merchant_image{"%06d" % randint(0, 999999)}.png'
                    store_indoor_copy_url = sync_img_to_local(f'http://ossmerchant.gdwxyf.com/{shop_id}/otherPhoto3.png', key=store_indoor_copy)
                    if store_indoor_copy_url:
                        MerchantImage.objects.create(merchant_id=merchant.id, image=store_indoor_copy)
                    merchantCheck = f'youhuiquanyi/{business_no}/merchant_image{"%06d" % randint(0, 999999)}.png'
                    merchantCheck_url = sync_img_to_local(f'http://ossmerchant.gdwxyf.com/{shop_id}/merchantCheck.png', key=merchantCheck)
                    if merchantCheck_url:
                        MerchantImage.objects.create(merchant_id=merchant.id, image=merchantCheck)
                merchantchannel_data = {
                    'merchant_id': merchant.id
                }
                if reaponse['allchannelJson']['channel_ruiyinxin']:
                    if reaponse['allchannelJson']['channel_ruiyinxin']['status'] == 1:
                        merchantchannel_data['smid'] = reaponse['allchannelJson']['channel_ruiyinxin']['merchant_other_4']
                        merchantchannel_data['channel_mchid'] = reaponse['allchannelJson']['channel_ruiyinxin']['merchant_other_7']
                        merchantchannel_data['channel_id'] = 2
                        merchantchannel_data['sub_mchid'] = reaponse['allchannelJson']['channel_ruiyinxin']['merchant_other_3']
                        MerchantChannel.objects.get_or_create(defaults=merchantchannel_data, merchant_id=merchant.id, channel_id=merchantchannel_data['channel_id'])
                if reaponse['allchannelJson']['channel_sxf_tq']:
                    if reaponse['allchannelJson']['channel_sxf_tq']['status'] == 1:
                        merchantchannel_data['smid'] = reaponse['allchannelJson']['channel_sxf_tq']['merchant_other_4'] if 'merchant_other_4' in reaponse['allchannelJson']['channel_sxf_tq'] else ''
                        merchantchannel_data['channel_mchid'] = reaponse['allchannelJson']['channel_sxf_tq']['merchant_no']
                        merchantchannel_data['channel_id'] = 3
                        merchantchannel_data['sub_mchid'] = reaponse['allchannelJson']['channel_sxf_tq']['merchant_other_3'] if 'merchant_other_3' in reaponse['allchannelJson']['channel_sxf_tq'] else ''
                        MerchantChannel.objects.get_or_create(defaults=merchantchannel_data, merchant_id=merchant.id, channel_id=merchantchannel_data['channel_id'])
                transaction.savepoint_commit(save_id)
            except Exception as e:
                print(e)
                order.order_status = 4
                order.save()
                return '获取商户信息失败'
    try:
        if merchant.subsidy_channel_id == 3 and merchant.is_sxf_subsidy == 0:
            subsidy_result = check_shop_can_subsidy(merchant.ruiyinxinMerchantID, 'sxf_tq')
            if subsidy_result['is_can_subsidy'] == 1:
                merchant.is_sxf_subsidy = 1
                merchant.save()
            elif subsidy_result['is_can_subsidy'] == 0:
                check_shop_can_subsidy(merchant.ruiyinxinMerchantID, 'ruiyinxin')
                merchant.subsidy_channel_id = 2
                merchant.is_ryx_subsidy = 1
                merchant.save()
    except:
        pass
    goods_name = order.goods_name
    goods_price = order.goods_price
    launchedgoods = LaunchedGoods.objects.filter(goods__name=goods_name, goods__price=goods_price, goods__merchant_id=merchant.id).first()
    if not launchedgoods:
        goods_data = {}
        goods_data['name'] = goods_name
        goods_data['create_time'] = datetime.datetime.now()
        goods_data['price'] = goods_price
        goods_data['cost_price'] = goods_price
        goods_data['market_price'] = goods_price
        default_image = f'minsu.png'
        goods_data['default_image'] = default_image
        goods_data['category_id'] = 1
        goods_data['is_launched'] = 0
        goods_data['merchant_id'] = merchant.id
        goods_data['order_species'] = 3
        goods = Goods.objects.create(**goods_data)
        launchedgoods_data = {
            'name': goods.name,
            'caption': goods.caption,
            'goods_id': goods.id,
            'category_id': 1,
            'point': goods.point,
            'price': goods.price,
            'is_launched': 0,
            'level_id': merchant.level_id,
            'create_time': goods.create_time
        }
        launchedgoods, create = LaunchedGoods.objects.get_or_create(
            defaults=launchedgoods_data,
            goods_id=goods.id,
            level_id=merchant.level_id
        )
        GoodsImage.objects.create(
            goods=goods,
            image=goods.default_image
        )
    order_data = {}
    order_data['orderNum'] = shop_order_id
    order_data['ownership'] = order.merchant_name
    order_data['order_id'] = shop_order_id
    order_data['create_time'] = order.create_time
    order_data['state'] = 3
    order_data['platform'] = 1
    riuser = CustomerUser.objects.filter(mobile=buyer_mobile).first()
    if not riuser:
        user_data = {}
        user_data['mobile'] = buyer_mobile
        user_data['is_distributor'] = 0
        user_data['realname'] = buyer_name
        user_data['username'] = buyer_name
        user_data['is_offline'] = 1
        user_data['create_time'] =datetime.datetime.now()
        riuser, create = CustomerUser.objects.get_or_create(mobile=user_data['mobile'], defaults=user_data)
    order_data['customer_user_id'] = riuser.id
    order_data['total_point'] = 0
    order_data['total_price'] = order.amount
    order_data['realpay_amount'] = order.amount
    order_data['payable_settled_amount'] = order.amount
    order_data['total_count'] = order.goods_count
    order_data['payment_time'] = order.create_time
    order_data['use_time'] = order.success_time
    order_data['level_id'] = launchedgoods.level_id
    order_data['state'] = 3
    order_data['settlement_state'] = 4
    order_data['merchant_id'] = merchant.id
    order_data['order_species'] = 3
    instance, create = OrderInfo.objects.get_or_create(orderNum=order_data['orderNum'], defaults=order_data)
    if create:
        ordergoods = OrderGoods.objects.create(
            order=instance,
            launched_goods=launchedgoods,
            count=order.goods_count,
            price=launchedgoods.goods.price,
            point=launchedgoods.goods.point,
            goods_id=launchedgoods.goods_id,
            merchant_id=launchedgoods.goods.merchant_id
        )
        # coupons_id = '%06d' % randint(0, 999999) + '%06d' % randint(0, 999999) + '%04d' % randint(0, 999999)
        # GoodsCoupons.objects.create(
        #     coupons_id=coupons_id,
        #     status=2,
        #     goods_id=ordergoods.goods_id,
        #     order_id=instance.id,
        #     validity_time=datetime.datetime.now(),
        #     use_time=order.success_time
        # )
        add_orderinfo_index.delay({'id': order.id})
        print(instance.id)
    order.order_status = 3
    order.merchant_id = merchant.id
    order.level_id = merchant.level_id
    order.order_id = instance.id
    order.save()
    return '添加民宿订单成功'


def order_pay_transactions_refund():
    queryset = OrderInfo.objects.filter(id__in=[3474122])
    for order_info in queryset:
        order_info.state = 8
        data_json = {}
        out_refund_no = timezone.now().strftime('%Y%m%d%H%M%S') + ('%06d' % randint(0, 999999)) + ('%09d' % order_info.customer_user_id)
        data_json['transaction_id'] = order_info.trade_no
        data_json['out_refund_no'] = out_refund_no
        data_json['notify_url'] = 'https://api.sutpay.com/cpay/refund_result/'
        data_json['amount'] = {"refund": int(8800), "total": int(8800), "currency": "CNY"}
        data_dict = pay_transactions_refund(data_json)
        order_info.out_refund_no = out_refund_no
        order_info.refund_id = data_dict['refund_id']
        # order_info.payment_time = order_data['success_time'].replace('T', ' ').replace('+08:00', '')
        # order_info.trade_no = order_data['transaction_id']
        # order_info.realpay_amount = Decimal(str(order_data['amount']['payer_total'])) / 100
        order_info.save()
        ordergoods_set = order_info.goodscoupons_set.filter(status=1).update(status=4)
        print(order_info.id)


    print(len(queryset))


@shared_task
def dispose_transfers_result(data):
    instance = CashActivityData.objects.get(id=data['id'])
    user = instance.companyuser
    if instance.state != 1:
        return '订单状态有误,拦截派发'
    request_data = {}
    activity = instance.cash_activity
    if instance.mchid == 'wx':
        params = {
            'mch_appid': instance.appid,
            'mchid': '1486198522',  # 商户号
            'nonce_str': instance.nonce_str,
            'partner_trade_no': instance.partner_trade_no,
            'openid': instance.openid,
            'check_name': 'NO_CHECK',
            'amount': int(instance.amount*100),
            'desc': instance.desc
        }
        data_dict = mmpaymkttransfers(params)
        if data_dict['result_code'] == 'SUCCESS':
            instance.received_time = data_dict['payment_time']
            instance.payment_no = data_dict['payment_no']
            instance.state = 2
            instance.channel_id = activity.wx_channel_id
            instance.save()
            request_data['code'] = 200
            request_data['message'] = '付款成功'
            if user.id in [1, 24]:
                text_data = {}
                text_data['payment_no'] = data_dict['payment_no']
                text_data['payment_time'] = data_dict['payment_time']
                text_data['out_biz_no'] = instance.out_biz_no
                text_data['amount'] = str(instance.amount)
                text_data['create_time'] = instance.create_time.strftime('%Y-%m-%d %H:%M:%S')
                ciphertext = encrypt_pkcs7_padding(json.dumps(text_data), user.authen_key)
                request_data['ciphertext'] = ciphertext
            else:
                request_data['payment_no'] = data_dict['payment_no']
                request_data['payment_time'] = data_dict['payment_time']
                request_data['out_biz_no'] = instance.out_biz_no
                request_data['amount'] = str(instance.amount)
                request_data['create_time'] = instance.create_time.strftime('%Y-%m-%d %H:%M:%S')
        else:
            instance.return_msg = data_dict['err_code_des']
            instance.state = 3
            instance.received_time = datetime.datetime.now()
            instance.channel_id = activity.wx_channel_id
            instance.save()
            request_data['code'] = 400
            request_data['message'] = '付款失败'
            if user.id in [1, 24]:
                text_data = {}
                text_data['err_code_des'] = data_dict['err_code_des']
                text_data['out_biz_no'] = instance.out_biz_no
                text_data['amount'] = str(instance.amount)
                text_data['create_time'] = instance.create_time.strftime('%Y-%m-%d %H:%M:%S')
                ciphertext = encrypt_pkcs7_padding(json.dumps(text_data), user.authen_key)
                request_data['ciphertext'] = ciphertext
            else:
                request_data['err_code_des'] = data_dict['err_code_des']
                request_data['out_biz_no'] = instance.out_biz_no
                request_data['amount'] = str(instance.amount)
                request_data['create_time'] = instance.create_time.strftime('%Y-%m-%d %H:%M:%S')
            cash_activity_id = instance.cash_activity_id
            amount = instance.amount
            with transaction.atomic():
                save_id = transaction.savepoint()
                try:
                    while True:
                        activity = CashActivity.objects.get(id=cash_activity_id)
                        activity_origin_take_amount = activity.take_amount
                        activity_origin_remain_amount = activity.remain_amount
                        activity_new_take_amount = activity_origin_take_amount - amount
                        activity_new_remain_amount = activity_origin_remain_amount + amount
                        result = CashActivity.objects.filter(take_amount=activity_origin_take_amount,
                                                             remain_amount=activity_origin_remain_amount,
                                                             id=cash_activity_id).update(
                            take_amount=activity_new_take_amount, remain_amount=activity_new_remain_amount)
                        if result == 0:
                            continue
                        break
                    transaction.savepoint_commit(save_id)
                except Exception as e:
                    transaction.savepoint_rollback(save_id)
    else:
        alipay_obj = C_AlipayOpen()
        trans_amount = str(instance.amount)
        order_title = instance.desc
        out_biz_no = instance.partner_trade_no
        if instance.ali_user_id:
            identity = instance.ali_user_id
            if activity.ali_channel_id == 8:
                result = json.loads(alipay_obj.transunitransfer_user_id_jiangxi88(identity, out_biz_no, trans_amount, order_title))
            elif activity.ali_channel_id == 11:
                result = json.loads(alipay_obj.transunitransfer_user_id_jiangxi88_jizhangben(identity, out_biz_no, trans_amount, order_title))
            else:
                result = json.loads(alipay_obj.transunitransfer_user_id(identity, out_biz_no, trans_amount, order_title))
        else:
            identity = instance.login_id
            realname = instance.realname
            if activity.ali_channel_id == 8:
                result = json.loads(alipay_obj.transunitransfer_login_id_jiangxi88(identity, realname, out_biz_no, trans_amount, order_title))
            elif activity.ali_channel_id == 11:
                result = json.loads(alipay_obj.transunitransfer_login_id_jiangxi88_jizhangben(identity, realname, out_biz_no, trans_amount, order_title))
            else:
                result = json.loads(alipay_obj.transunitransfer_login_id(identity, realname, out_biz_no, trans_amount, order_title))
        if result['code'] != '10000':
            instance.return_msg = result['sub_msg']
            instance.state = 3
            instance.received_time = datetime.datetime.now()
            instance.channel_id = activity.ali_channel_id
            instance.save()
            request_data['code'] = 400
            request_data['message'] = '付款失败'
            if user.id in [1, 24]:
                text_data = {}
                text_data['err_code_des'] = result['sub_msg']
                text_data['out_biz_no'] = instance.out_biz_no
                text_data['amount'] = str(instance.amount)
                text_data['create_time'] = instance.create_time.strftime('%Y-%m-%d %H:%M:%S')
                ciphertext = encrypt_pkcs7_padding(json.dumps(text_data), user.authen_key)
                request_data['ciphertext'] = ciphertext
            else:
                request_data['err_code_des'] = result['sub_msg']
                request_data['out_biz_no'] = instance.out_biz_no
                request_data['amount'] = str(instance.amount)
                request_data['create_time'] = instance.create_time.strftime('%Y-%m-%d %H:%M:%S')
            cash_activity_id = instance.cash_activity_id
            amount = instance.amount
            with transaction.atomic():
                save_id = transaction.savepoint()
                try:
                    while True:
                        activity = CashActivity.objects.get(id=cash_activity_id)
                        activity_origin_take_amount = activity.take_amount
                        activity_origin_remain_amount = activity.remain_amount
                        activity_new_take_amount = activity_origin_take_amount - amount
                        activity_new_remain_amount = activity_origin_remain_amount + amount
                        result = CashActivity.objects.filter(take_amount=activity_origin_take_amount,
                                                             remain_amount=activity_origin_remain_amount,
                                                             id=cash_activity_id).update(
                            take_amount=activity_new_take_amount, remain_amount=activity_new_remain_amount)
                        if result == 0:
                            continue
                        break
                    transaction.savepoint_commit(save_id)
                except Exception as e:
                    transaction.savepoint_rollback(save_id)
        else:
            instance.received_time = result['trans_date']
            instance.payment_no = result['pay_fund_order_id']
            instance.state = 2
            instance.channel_id = activity.ali_channel_id
            instance.save()
            request_data['code'] = 200
            request_data['message'] = '付款成功'
            if user.id in [1, 24]:
                text_data = {}
                text_data['payment_no'] = result['pay_fund_order_id']
                text_data['payment_time'] = result['trans_date']
                text_data['out_biz_no'] = instance.out_biz_no
                text_data['amount'] = str(instance.amount)
                text_data['create_time'] = instance.create_time.strftime('%Y-%m-%d %H:%M:%S')
                ciphertext = encrypt_pkcs7_padding(json.dumps(text_data), user.authen_key)
                request_data['ciphertext'] = ciphertext
            else:
                request_data['payment_no'] = result['pay_fund_order_id']
                request_data['payment_time'] = result['trans_date']
                request_data['out_biz_no'] = instance.out_biz_no
                request_data['amount'] = str(instance.amount)
                request_data['create_time'] = instance.create_time.strftime('%Y-%m-%d %H:%M:%S')
    try:
        print(request_data)
        if instance.notify_url:
            headers = {'Content-Type': 'application/json'}
            response = requests.request('post', url=instance.notify_url, data=json.dumps(request_data), headers=headers)
    except Exception as e:
        print(e)
        pass
    return '写入成功'


@shared_task
def add_orderinfo_index(data):
    order_id = data['id']
    orderinfo = OrderInfo.objects.get(id=order_id)
    merchant = orderinfo.merchant
    ordergoods_set = OrderGoods.objects.filter(order_id=orderinfo.id)
    level = orderinfo.level
    if orderinfo.customer_user:
        mobile = orderinfo.customer_user.mobile
    elif orderinfo.customeruservice:
        mobile = orderinfo.customeruservice.mobile
    else:
        mobile = ''
    if level:
        prov_id = level.prov_id
        city_id = level.city_id
        district_id = level.district_id
        branch_id = level.branch_id
    else:
        prov_id = ''
        city_id = ''
        district_id = ''
        branch_id = ''
    goods_name = ''
    for ordergoods in ordergoods_set:
        goods = ordergoods.goods
        goods_name += goods.name
    text = f'{merchant.name} {orderinfo.orderNum} {orderinfo.order_id} {mobile} {merchant.wyfMerchantID} {goods_name}'
    body = [{
        "_index": "equity_mall_order",
        "_id": orderinfo.id,
        "_type": 'modelresult',
        "action": 'create',
        "_source": {
            "text": text,
            "prov_id": prov_id,
            "city_id": city_id,
            "district_id": district_id,
            "branch_id": branch_id,
            "business_no": merchant.wyfMerchantID
        }
    }]
    es = Elasticsearch(["http://159.75.55.19:9200/"])
    result = helpers.bulk(es, body)
    return result



if __name__ == '__main__':
    pass
    # get_residentialhostel_orderinfo({'id': 47})
    # for id in [4243834,4244789,4245410,4245426,4247370,4248674,4250076,4251059,4251874,4251880,4251885,4251891,4253425,4255708,4256184,4256485,4257201,4257205,4257269,4257628,4257659,4258255,4258258,4258275,4259056,4259619,4260249,4261780,4266937,4266947,4267345,4267346,4267350,4267353,4267355,4267436,4267535,4267537,4268045,4268145,4268402,4268419,4268424,4268426,4268428,4268539,4268702,4268704,4269199,4270225,4270348,4274748,4274939,4275764,4277955,4282442,4283030,4283647,4283658,4285581,4287094,4287597,4288380,4288503,4288633,4288635,4288713,4288846,4289043,4289284,4290133,4290137,4291072,4291077,4291495,4291662,4292018,4292150,4293283,4293468,4294533,4294537,4294542,4294551,4294555,4299100,4299146,4299147,4299150,4299156,4299163,4299659,4300272,4300354,4300475,4300536,4300538,4300559,4300829,4300903,4300966,4301031,4301033,4301197,4301223,4301361,4301545,4301741,4301772,4301775,4301844,4302838,4303459,4303467,4303975,4305016,4305667,4308315,4310961,4312713,4312721,4312754,4314082,4316516,4316616,4316620,4317624,4317974,4319446,4319457,4319832,4320007,4320540,4320906,4321649,4321655,4321703,4322312,4322676,4322899,4323055,4324700,4328825,4329942,4330172,4330176,4330265,4330829,4331943,4332015,4333523,4334479,4334954,4337046,4337533,4338141,4338387,4338407,4338450,4339089,4339105,4339195,4339196,4339500,4339501,4339657,4339677,4342984,4343063,4343064,4345252,4345259,4345262,4345821,4348679,4349138,4350376,4352627,4352631,4353096,4353450,4353451,4353769,4355019,4357287,4357626,4359272,4359288,4359289,4359294,4359296,4360765,4361556,4362199,4362217,4362722,4362723,4363437,4363549,4363637,4363638,4363710,4363720,4363839,4363895,4364329,4370205,4370704,4371223,4371238,4371310,4372821,4375518,4375519,4375520,4375521,4375522,4375523,4375524,4375525,4375526,4375527,4375528,4375530,4375531,4375533,4375534,4375535,4375536,4375537,4375538,4375539,4375540,4375542,4375543,4375544,4375545,4375546,4375547,4375548,4375549,4375550,4375566,4375567,4375569,4375570,4375571,4375572,4375573,4375575,4375576,4375577,4375578,4375580,4375581,4375582,4375583,4375584,4375585,4375586,4375587,4375588,4375589,4375590,4375591,4375592,4375593,4375594,4375595,4375596,4375597,4375598,4375599,4375600,4375601,4375602,4375603,4375604,4375605,4375606,4375608,4375609,4375610,4375611,4375612,4375613,4375614,4375615,4375616,4375617,4375618,4375619,4375620,4375621,4375622,4375623,4375624,4375625,4375626,4375627,4375628,4375629,4375630,4375632,4375633,4375634,4375636,4375637,4375638,4375639,4375640,4375641,4375642,4375643,4375644,4375646,4375647,4375648,4375649,4375650,4375651,4375652,4375653,4375654,4375655,4375656,4375657,4375658,4375659,4375660,4375661,4375662,4375664,4375665,4375666,4375667,4375668,4375669,4375671,4375672,4375673,4375674,4375675,4375676,4375677,4375678,4375679,4375682,4375683,4375684,4375686,4375688,4375689,4375690,4375691,4375693,4375694,4375695,4375696,4375699,4375700,4375701,4375702,4375704,4375705,4375706,4375707,4375708,4375710,4375711,4375713,4375714,4375715,4375716,4375717,4375718,4375719,4375721,4375722,4375723,4375724,4375725,4375727,4375728,4375729,4375731,4375732,4375735,4375736,4375737,4375738,4375739,4375743,4375746,4375747,4375748,4375749,4375750,4375752,4375753,4375754,4375755,4375756,4375757,4375758,4375759,4375760,4375761,4375764,4375765,4375766,4375768,4375769,4375770,4375772,4375774,4375775,4375776,4375777,4375778,4375779,4375780,4375795,4375796,4375797,4375798,4375799,4375800,4375804,4375805,4375807,4375808,4375809,4375810,4375811,4375812,4375813,4375814,4375815,4375816,4375819,4375821,4375823,4375824,4375825,4375827,4375829,4375830,4375832,4375833,4375834,4375835,4375836,4375837,4375839,4375840,4375841,4375843,4375844,4375845,4375846,4375847,4375848,4375850,4375851,4375852,4375853,4375854,4375856,4375857,4375858,4375859,4375860,4375861,4375862,4375863,4375864,4375865,4375866,4375867,4375868,4375869,4375870,4375871,4375872,4375873,4375874,4375875,4375876,4375877,4375878,4375879,4375881,4375882,4375884,4375885,4375886,4375887,4375888,4375889,4375891,4375892,4375894,4375895,4375896,4375897,4375899,4375900,4375901,4375902,4375903,4375904,4375905,4375906,4375907,4375908,4375909,4375910,4375911,4375912,4375913,4375914,4375915,4375917,4375918,4375919,4375920,4375921,4375922,4375923,4375924,4375925,4375926,4375927,4375928,4375929,4375932,4375933,4375934,4375936,4375937,4375938,4375947,4375948,4375949,4375951,4375953,4375954,4375955,4375957,4375958,4375960,4375961,4375962,4375963,4375964,4375965,4375966,4375967,4375968,4375970,4375971,4375972,4375973,4375975,4375976,4375977,4375978,4375979,4375980,4375981,4375982,4375983,4375985,4375986,4375987,4375988,4375989,4375990,4375991,4375993,4375994,4375995,4375996,4375997,4375998,4375999,4376000,4376001,4376002,4376003,4376004,4376005,4376007,4376008,4376009,4376010,4376011,4376013,4376014,4376015,4376016,4376028,4376029,4376030,4376031,4376032,4376033,4376034,4376035,4376036,4376039,4376040,4376041,4376042,4376043,4376044,4376045,4376047,4376050,4376051,4376052,4376053,4376054,4376055,4376056,4376057,4376058,4376060,4376061,4376062,4376063,4376064,4376065,4376066,4376067,4376068,4376069,4376070,4376071,4376073,4376074,4376075,4376076,4376077,4376079,4376080,4376081,4376082,4376083,4376084,4376085,4376087,4376088,4376089,4376090,4376091,4376092,4376093,4376094,4376096,4376097,4376098,4376099,4376100,4376101,4376102,4376103,4376105,4376107,4376108,4376109,4376111,4376112,4376114,4376115,4376117,4376118,4376120,4376121,4376122,4376123,4376124,4376125,4376126,4376127,4376129,4376130,4376132,4376134,4376135,4376136,4376138,4376139,4376141,4376142,4376143,4376144,4376145,4376146,4376147,4376148,4376149,4376150,4376151,4376152,4376153,4376154,4376155,4376156,4376157,4376158,4376159,4376160,4376162,4376163,4376164,4376166,4376167,4376168,4376169,4376171,4376178,4376179,4376182,4376184,4376185,4376186,4376187,4376189,4376190,4376191,4376192,4376193,4376194,4376195,4376196,4376197,4376198,4376199,4376201,4376202,4376203,4376204,4376205,4376206,4376207,4376208,4376209,4376210,4376211,4376212,4376245,4376246,4376247,4376249,4376250,4376251,4376252,4376253,4376254,4376255,4376256,4376258,4376259,4376260,4376261,4376262,4376263,4376264,4376265,4376266,4376267,4376268,4376269,4376270,4376271,4376272,4376274,4376275,4376277,4376278,4376279,4376280,4376281,4376283,4376284,4376285,4376286,4376287,4376288,4376289,4376291,4376293,4376294,4376295,4376297,4376298,4376299,4376300,4376301,4376302,4376303,4376304,4376305,4376306,4376307,4376308,4376309,4376310,4376311,4376313,4376314,4376315,4376316,4376317,4376318,4376319,4376320,4376321,4376323,4376324,4376326,4376327,4376329,4376330,4376331,4376332,4376334,4376336,4376339,4376340,4376341,4376343,4376344,4376345,4376347,4376348,4376349,4376350,4376351,4376352,4376353,4376354,4376355,4376358,4376359,4376360,4376361,4376362,4376363,4376364,4376366,4376368,4376369,4376370,4376371,4376372,4376373,4376374,4376375,4376376,4376378,4376379,4376380,4376381,4376382,4376383,4376384,4376385,4376386,4376388,4376390,4376391,4376392,4376393,4376394,4376395,4376397,4376398,4376399,4376400,4376402,4376403,4376405,4376406,4376407,4376408,4376409,4376410,4376411,4376412,4376413,4376414,4376415,4376416,4376417,4376418,4376419,4376420,4376422,4376423,4376425,4376426,4376427,4376429,4376430,4376431,4376432,4376433,4376434,4376435,4376436,4376437,4376438,4376440,4376441,4376442,4376443,4376444,4376446,4376447,4376449,4376450,4376451,4376452,4376453,4376455,4376456,4376457,4376458,4376460,4376488,4376489,4376491,4376492,4376494,4376495,4376497,4376498,4376500,4376501,4376502,4376503,4376504,4376505,4376506,4376507,4376510,4376511,4376512,4376513,4376514,4376515,4376516,4376517,4376518,4376519,4376520,4376521,4376522,4376523,4376524,4376525,4376527,4376528,4376529,4376530,4376531,4376533,4376534,4376535,4376537,4376538,4376539,4376541,4376542,4376543,4376544,4376545,4376546,4376547,4376548,4376549,4376550,4376551,4376552,4376553,4376554,4376555,4376556,4376557,4376558,4376559,4376560,4376561,4376562,4376563,4376564,4376565,4376566,4376568,4376569,4376570,4376572,4376573,4376575,4376576,4376578,4376579,4376581,4376582,4376583,4376584,4376585,4376586,4376587,4376588,4376590,4376591,4376592,4376593,4376594,4376595,4376596,4376597,4376599,4376600,4376601,4376602,4376603,4376605,4376606,4376607,4376609,4376610,4376611,4376612,4376614,4376615,4376616,4376617,4376618,4376619,4376620,4376621,4376623,4376624,4376625,4376626,4376627,4376629,4376631,4376632,4376633,4376634,4376635,4376636,4376637,4376638,4376639,4376640,4376641,4376642,4376643,4376644,4376645,4376646,4376647,4376648,4376649,4376650,4376651,4376652,4376654,4376655,4376663,4376664,4376666,4376667,4376668,4376669,4376694,4376695,4376696,4376697,4376699,4376700,4376701,4376702,4376703,4376705,4376706,4376707,4376709,4376710,4376711,4376714,4376715,4376717,4376719,4376720,4376721,4376722,4376724,4376725,4376726,4376727,4376728,4376729,4376730,4376731,4376732,4376733,4376734,4376735,4376736,4376737,4376738,4376739,4376740,4376741,4376742,4376743,4376745,4376746,4376748,4376749,4376751,4376752,4376753,4376756,4376757,4376758,4376759,4376760,4376761,4376762,4376763,4376764,4376765,4376767,4376768,4376769,4376770,4376771,4376773,4376774,4376775,4376776,4376777,4376778,4376779,4376780,4376781,4376782,4376783,4376784,4376785,4376786,4376787,4376788,4376789,4376790,4376791,4376793,4376794,4376796,4376797,4376798,4376799,4376801,4376802,4376803,4376804,4376805,4376806,4376807,4376808,4376809,4376810,4376811,4376812,4376813,4376814,4376816,4376817,4376818,4376819,4376820,4376821,4376822,4376823,4376824,4376825,4376827,4376828,4376829,4376831,4376832,4376833,4376834,4376835,4376836,4376837,4376838,4376839,4376840,4376841,4376842,4376844,4376845,4376846,4376847,4376848,4376849,4376851,4376852,4376853,4376854,4376855,4376856,4376857,4376859,4376860,4376861,4376862,4376863,4376864,4376866,4376867,4376868,4376869,4376870,4376871,4376872,4376873,4376874,4376875,4376876,4376877,4376878,4376880,4376881,4376882,4376883,4376884,4376885,4376886,4376887,4376888,4376889,4376892,4376894,4376895,4376896,4376897,4376898,4376899,4376900,4376901,4376902,4376918,4376919,4376920,4376921,4376922,4376923,4376924,4376926,4376928,4376929,4376930,4376931,4376932,4376933,4376934,4376935,4376936,4376937,4376939,4376940,4376941,4376942,4376943,4376944,4376945,4376946,4376947,4376948,4376950,4376952,4376955,4376956,4376957,4376958,4376962,4376963,4376964,4376965,4376966,4376967,4376969,4376971,4376972,4376973,4376975,4376976,4376977,4376978,4376979,4376980,4376981,4376982,4376983,4376984,4376985,4376986,4376988,4376989,4376990,4376991,4376992,4376993,4376994,4376995,4376996,4376997,4376998,4376999,4377000,4377002,4377003,4377004,4377005,4377006,4377007,4377008,4377009,4377011,4377012,4377013,4377014,4377016,4377017,4377018,4377019,4377020,4377021,4377022,4377024,4377026,4377027,4377028,4377030,4377031,4377032,4377033,4377034,4377036,4377037,4377038,4377039,4377040,4377041,4377042,4377045,4377047,4377048,4377049,4377050,4377051,4377052,4377054,4377055,4377056,4377057,4377058,4377059,4377060,4377062,4377063,4377064,4377065,4377067,4377068,4377069,4377070,4377071,4377072,4377073,4377074,4377075,4377076,4377077,4377078,4377079,4377080,4377082,4377083,4377084,4377085,4377086,4377087,4377088,4377089,4377090,4377091,4377092,4377093,4377094,4377095,4377096,4377097,4377098,4377099,4377100,4377102,4377103,4377105,4377106,4377107,4377108,4377109,4377110,4377111,4377112,4377113,4377114,4377115,4377116,4377117,4377118,4377120,4377121,4377122,4377123,4377124,4377125,4377126,4377127,4377147,4377148,4377149,4377150,4377151,4377152,4377153,4377154,4377155,4377156,4377157,4377158,4377159,4377160,4377161,4377163,4377164,4377165,4377167,4377168,4377169,4377170,4377171,4377172,4377173,4377175,4377176,4377177,4377178,4377179,4377180,4377181,4377183,4377184,4377185,4377186,4377187,4377188,4377189,4377190,4377191,4377192,4377193,4377194,4377195,4377196,4377197,4377199,4377200,4377201,4377202,4377203,4377204,4377205,4377207,4377208,4377210,4377211,4377213,4377214,4377215,4377216,4377217,4377218,4377219,4377220,4377222,4377223,4377224,4377225,4377226,4377227,4377228,4377230,4377231,4377232,4377233,4377235,4377236,4377237,4377238,4377239,4377240,4377242,4377243,4377245,4377247,4377248,4377250,4377251,4377253,4377254,4377255,4377256,4377257,4377258,4377259,4377261,4377262,4377264,4377265,4377266,4377267,4377268,4377269,4377270,4377271,4377272,4377273,4377274,4377275,4377276,4377277,4377279,4377280,4377282,4377283,4377284,4377285,4377286,4377288,4377290,4377291,4377292,4377293,4377294,4377295,4377296,4377297,4377298,4377299,4377300,4377301,4377309,4377310,4377311,4377312,4377313,4377315,4377316,4377318,4377319,4377320,4377321,4377322,4377325,4377326,4377327,4377328,4377329,4377330,4377331,4377332,4377333,4377334,4377335,4377336,4377337,4377339,4377341,4377342,4377343,4377344,4377365,4377366,4377367,4377368,4377369,4377370,4377371,4377372,4377373,4377374,4377375,4377376,4377377,4377378,4377379,4377380,4377381,4377384,4377385,4377386,4377387,4377388,4377390,4377391,4377392,4377394,4377395,4377397,4377398,4377399,4377400,4377401,4377402,4377403,4377404,4377405,4377406,4377407,4377409,4377410,4377411,4377412,4377414,4377415,4377416,4377417,4377418,4377419,4377420,4377421,4377422,4377423,4377424,4377425,4377426,4377427,4377428,4377429,4377430,4377431,4377432,4377433,4377434,4377435,4377438,4377439,4377441,4377442,4377443,4377444,4377445,4377446,4377447,4377448,4377449,4377450,4377452,4377454,4377455,4377456,4377457,4377458,4377459,4377460,4377461,4377462,4377463,4377464,4377465,4377466,4377467,4377468,4377469,4377470,4377471,4377472,4377473,4377474,4377475,4377476,4377477,4377478,4377479,4377481,4377482,4377483,4377484,4377485,4377486,4377487,4377488,4377489,4377490,4377491,4377492,4377493,4377495,4377497,4377499,4377500,4377501,4377502,4377503,4377504,4377505,4377506,4377507,4377508,4377509,4377511,4377512,4377517,4377518,4377519,4377520,4377521,4377522,4377523,4377524,4377525,4377526,4377528,4377530,4377531,4377532,4377533,4377534,4377535,4377536,4377537,4377538,4377539,4377540,4377541,4377542,4377543,4377545,4377546,4377548,4377549,4377551,4377552,4377553,4377554,4377555,4377583,4377584,4377585,4377586,4377587,4377588,4377589,4377590,4377592,4377593,4377594,4377595,4377596,4377597,4377598,4377599,4377600,4377601,4377603,4377605,4377606,4377607,4377609,4377610,4377611,4377612,4377613,4377614,4377615,4377616,4377618,4377619,4377620,4377622,4377623,4377624,4377625,4377627,4377628,4377629,4377630,4377631,4377635,4377637,4377638,4377639,4377640,4377641,4377642,4377643,4377644,4377645,4377646,4377647,4377648,4377649,4377650,4377651,4377653,4377654,4377655,4377657,4377658,4377659,4377660,4377661,4377662,4377664,4377666,4377667,4377668,4377669,4377670,4377671,4377672,4377673,4377674,4377676,4377677,4377678,4377680,4377681,4377683,4377684,4377685,4377686,4377687,4377688,4377689,4377690,4377691,4377692,4377693,4377694,4377695,4377696,4377697,4377698,4377699,4377700,4377701,4377702,4377703,4377704,4377706,4377707,4377708,4377710,4377711,4377712,4377713,4377714,4377715,4377717,4377718,4377719,4377720,4377722,4377723,4377725,4377726,4377728,4377729,4377730,4377731,4377732,4377733,4377734,4377735,4377737,4377745,4377747,4377748,4377749,4377750,4377751,4377752,4377753,4377754,4377755,4377757,4377758,4377759,4377761,4377764,4377765,4377766,4377767,4377768,4377769,4377770,4377771,4377773,4377774,4377775,4377776,4377777,4377778,4377798,4377799,4377800,4377801,4377803,4377804,4377805,4377806,4377807,4377808,4377809,4377810,4377811,4377812,4377813,4377814,4377815,4377816,4377817,4377819,4377820,4377821,4377822,4377824,4377825,4377826,4377827,4377828,4377829,4377830,4377831,4377832,4377833,4377834,4377836,4377837,4377838,4377839,4377840,4377841,4377842,4377843,4377845,4377846,4377847,4377849,4377850,4377851,4377852,4377853,4377854,4377855,4377856,4377857,4377858,4377859,4377860,4377861,4377862,4377863,4377864,4377865,4377866,4377867,4377868,4377869,4377870,4377871,4377872,4377873,4377874,4377875,4377877,4377878,4377879,4377880,4377881,4377882,4377883,4377884,4377886,4377888,4377889,4377890,4377891,4377892,4377893,4377894,4377895,4377896,4377897,4377898,4377899,4377901,4377903,4377904,4377922,4377923,4377924,4377925,4377926,4377927,4377928,4377929,4377931,4377932,4377933,4377934,4377936,4377937,4377939,4377940,4377941,4377942,4377944,4377946,4377947,4377948,4377949,4377950,4377951,4377952,4377953,4377954,4377955,4377957,4377958,4377959,4377961,4377962,4377963,4377964,4377966,4377967,4377968,4377992,4377994,4377995,4377996,4377997,4377998,4377999,4378000,4378001,4378002,4378003,4378005,4378006,4378007,4378008,4378009,4378011,4378012,4378014,4378015,4378016,4378017,4378018,4378019,4378020,4378021,4378023,4378024,4378025,4378026,4378027,4378029,4378030,4378031,4378032,4378033,4378034,4378035,4378036,4378037,4378038,4378039,4378040,4378058,4378060,4378061,4378062,4378064,4378065,4378066,4378068,4378069,4378070,4378071,4378072,4378073,4378074,4378076,4378077,4378078,4378079,4378082,4378083,4378095,4378096,4378097,4378098,4378099,4378100,4378101,4378102,4378103,4378135,4378136,4378137,4378138,4378139,4378159,4378160,4378161,4378162,4378163,4378164,4378188,4378189,4380856,4381550,4381554,4383296,4385182,4386226,4386227,4387902,4387904,4387905,4387906,4387907,4387909,4387910,4387911,4387912,4387914,4387915,4387916,4387917,4387918,4387919,4387921,4387922,4387923,4387924,4387926,4387928,4387929,4387931,4387932,4387933,4387934,4387935,4387937,4387938,4387939,4387940,4387941,4387942,4387944,4387946,4387947,4387948,4387949,4387950,4387951,4387952,4387954,4387955,4387956,4387957,4387958,4387959,4387960,4387961,4387962,4387963,4387964,4387965,4387967,4387968,4387969,4387970,4387972,4387974,4387975,4387977,4387978,4387979,4387980,4387983,4387984,4387985,4387986,4387989,4387990,4387991,4387992,4387993,4387994,4387995,4387996,4387997,4387998,4387999,4388002,4388003,4388004,4388005,4388007,4388008,4388009,4388010,4388011,4388012,4388013,4388014,4388015,4388016,4388017,4388018,4388019,4388020,4388022,4388023,4388026,4388027,4388028,4388030,4388031,4388032,4388034,4388036,4388038,4388040,4388041,4388042,4388043,4388044,4388045,4388046,4388047,4388048,4388050,4388051,4388052,4388053,4388054,4388056,4388059,4388061,4388062,4388063,4388065,4388066,4388067,4388069,4388070,4388071,4388072,4388073,4388074,4388075,4388077,4388078,4388079,4388080,4388082,4388084,4388085,4388087,4388089,4388093,4388094,4388095,4388096,4388162,4388163,4388164,4388166,4388167,4388168,4388169,4388170,4388173,4388175,4388176,4388177,4388178,4388179,4388180,4388181,4388182,4388183,4388184,4388185,4388186,4388187,4388189,4388191,4388192,4388193,4388194,4388196,4388197,4388198,4388199,4388201,4388202,4388203,4388205,4388206,4388207,4388208,4388209,4388210,4388211,4388212,4388213,4388214,4388215,4388216,4388217,4388219,4388220,4388221,4388222,4388223,4388224,4388225,4388227,4388228,4388229,4388231,4388233,4388234,4388236,4388237,4388239,4388240,4388241,4388242,4388243,4388244,4388245,4388247,4388249,4388250,4388251,4388253,4388255,4388256,4388257,4388258,4388260,4388262,4388265,4388266,4388268,4388269,4388270,4388271,4388272,4388273,4388275,4388276,4388277,4388279,4388280,4388283,4388285,4388286,4388287,4388289,4388290,4388291,4388292,4388295,4388296,4388298,4388300,4388301,4388302,4388305,4388306,4388308,4388309,4388310,4388311,4388312,4388316,4388317,4388319,4388320,4388321,4388323,4388324,4388325,4388327,4388328,4388330,4388333,4388334,4388335,4388337,4388338,4388339,4388340,4388341,4388342,4388343,4388344,4388345,4388346,4388347,4388348,4388349,4388351,4388352,4388353,4388354,4388355,4388356,4388357,4388358,4388359,4388360,4388361,4388362,4388364,4388365,4388366,4388367,4388368,4388369,4388370,4388372,4388373,4388374,4388376,4388378,4388380,4388382,4388383,4388384,4388385,4388386,4388388,4388390,4388391,4388392,4388396,4388397,4388398,4388400,4388402,4388451,4388452,4388453,4388454,4388455,4388457,4388458,4388459,4388460,4388461,4388462,4388463,4388464,4388465,4388466,4388467,4388468,4388469,4388472,4388473,4388474,4388475,4388476,4388478,4388479,4388480,4388481,4388482,4388483,4388484,4388485,4388486,4388487,4388488,4388490,4388491,4388493,4388494,4388495,4388497,4388498,4388500,4388501,4388502,4388503,4388505,4388506,4388507,4388508,4388509,4388510,4388511,4388513,4388514,4388515,4388516,4388518,4388520,4388521,4388522,4388523,4388524,4388525,4388527,4388529,4388530,4388531,4388533,4388534,4388535,4388537,4388539,4388540,4388541,4388542,4388543,4388544,4388545,4388546,4388547,4388548,4388549,4388550,4388551,4388552,4388554,4388555,4388556,4388559,4388560,4388562,4388564,4388565,4388566,4388567,4388568,4388569,4388570,4388571,4388572,4388573,4388574,4388575,4388576,4388577,4388578,4388579,4388580,4388581,4388582,4388584,4388585,4388586,4388587,4388588,4388589,4388590,4388591,4388592,4388593,4388595,4388596,4388597,4388599,4388600,4388601,4388602,4388603,4388604,4388605,4388606,4388608,4388609,4388610,4388611,4388612,4388614,4388615,4388617,4388618,4388619,4388620,4388621,4388623,4388624,4388625,4388626,4388628,4388630,4388631,4388636,4388637,4388639,4388640,4388641,4388642,4388643,4388644,4388645,4388646,4388647,4388648,4388649,4388650,4388651,4388652,4388654,4388655,4388656,4388657,4388659,4388660,4388661,4388662,4388663,4388664,4388666,4388667,4388669,4388670,4388722,4388724,4388725,4388726,4388727,4388728,4388729,4388730,4388731,4388732,4388733,4388734,4388736,4388737,4388738,4388739,4388741,4388743,4388744,4388745,4388747,4388748,4388750,4388751,4388752,4388753,4388754,4388756,4388757,4388758,4388759,4388760,4388762,4388763,4388764,4388765,4388766,4388767,4388768,4388769,4388770,4388773,4388774,4388775,4388776,4388778,4388779,4388780,4388782,4388784,4388785,4388786,4388787,4388788,4388789,4388790,4388791,4388794,4388795,4388796,4388797,4388798,4388800,4388802,4388803,4388804,4388805,4388806,4388807,4388809,4388810,4388812,4388814,4388816,4388817,4388818,4388819,4388820,4388821,4388822,4388823,4388824,4388825,4388826,4388827,4388828,4388830,4388831,4388832,4388833,4388834,4388835,4388836,4388837,4388838,4388839,4388840,4388841,4388842,4388843,4388844,4388845,4388846,4388847,4388848,4388850,4388851,4388852,4388853,4388854,4388856,4388857,4388858,4388859,4388860,4388861,4388862,4388863,4388864,4388865,4388866,4388867,4388868,4388869,4388870,4388871,4388872,4388874,4388875,4388877,4388879,4388880,4388881,4388882,4388883,4388884,4388886,4388887,4388889,4388890,4388891,4388893,4388894,4388895,4388896,4388897,4388898,4388900,4388902,4388903,4388962,4388964,4388965,4388966,4388967,4388969,4388970,4388971,4388972,4388974,4388975,4388977,4388978,4388979,4388980,4388981,4388983,4388984,4388985,4388986,4388987,4388988,4388989,4388990,4388992,4388993,4388995,4388998,4388999,4389000,4389001,4389002,4389003,4389004,4389005,4389006,4389007,4389008,4389009,4389010,4389011,4389012,4389013,4389014,4389016,4389020,4389021,4389023,4389024,4389025,4389026,4389027,4389028,4389029,4389030,4389031,4389032,4389034,4389036,4389037,4389038,4389039,4389040,4389041,4389042,4389043,4389044,4389045,4389046,4389047,4389048,4389049,4389050,4389051,4389052,4389054,4389055,4389056,4389057,4389059,4389061,4389062,4389063,4389064,4389065,4389066,4389067,4389068,4389069,4389070,4389072,4389073,4389074,4389075,4389076,4389077,4389078,4389079,4389080,4389081,4389082,4389083,4389085,4389087,4389088,4389089,4389090,4389091,4389092,4389093,4389094,4389096,4389097,4389099,4389100,4389101,4389102,4389103,4389104,4389105,4389106,4389107,4389108,4389109,4389110,4389112,4389113,4389114,4389115,4389118,4389119,4389120,4389121,4389123,4389125,4389127,4389129,4389130,4389133,4389134,4389136,4389137,4389138,4389139,4389141,4389142,4389143,4389144,4389145,4389147,4389148,4389149,4389150,4389151,4389153,4389155,4389156,4389157,4389159,4389161,4389162,4389164,4389165,4389166,4389168,4389169,4389170,4389171,4389172,4389173,4389174,4389175,4389176,4389177,4389224,4389227,4389228,4389229,4389231,4389233,4389235,4389236,4389238,4389241,4389243,4389244,4389245,4389246,4389249,4389250,4389252,4389253,4389255,4389256,4389258,4389259,4389260,4389261,4389262,4389264,4389265,4389266,4389267,4389269,4389271,4389273,4389274,4389275,4389277,4389278,4389279,4389281,4389282,4389285,4389286,4389287,4389289,4389291,4389292,4389293,4389295,4389296,4389298,4389299,4389301,4389305,4389306,4389307,4389309,4389310,4389311,4389312,4389314,4389315,4389316,4389317,4389318,4389319,4389320,4389321,4389322,4389323,4389324,4389325,4389327,4389328,4389329,4389330,4389331,4389332,4389333,4389334,4389336,4389337,4389338,4389340,4389342,4389344,4389346,4389347,4389348,4389350,4389352,4389353,4389354,4389355,4389356,4389357,4389359,4389362,4389363,4389364,4389365,4389367,4389368,4389369,4389370,4389371,4389372,4389373,4389374,4389376,4389377,4389379,4389380,4389382,4389383,4389384,4389385,4389386,4389387,4389388,4389389,4389390,4389391,4389392,4389393,4389394,4389395,4389396,4389397,4389398,4389399,4389408,4389410,4389412,4389413,4389414,4389416,4389417,4389418,4389419,4389421,4389423,4389424,4389426,4389428,4389429,4389430,4389431,4389432,4389434,4389435,4389436,4389476,4389477,4389478,4389479,4389480,4389483,4389484,4389485,4389486,4389487,4389488,4389490,4389492,4389493,4389494,4389495,4389496,4389497,4389498,4389500,4389501,4389502,4389504,4389505,4389506,4389507,4389509,4389510,4389511,4389513,4389514,4389515,4389516,4389517,4389518,4389519,4389520,4389521,4389523,4389524,4389527,4389528,4389529,4389530,4389531,4389532,4389533,4389534,4389536,4389571,4389572,4389574,4389575,4389576,4389578,4389579,4389580,4389582,4389583,4389585,4389587,4389588,4389589,4389590,4389591,4389592,4389593,4389595,4389596,4389598,4389599,4389600,4389601,4389602,4389603,4389604,4389605,4389606,4389607,4389636,4389638,4389641,4389642,4389643,4389644,4389645,4389648,4389649,4389650,4389652,4389706,4389707,4389708,4389709,4389710,4389712,4389714,4389715,4389716,4389718,4389719,4389763,4389764,4389765,4390890,4393184,4393239,4393277,4396808,4397356,4398053,4398056,4398729,4398824,4398839,4398849,4398856,4399392,4399393,4402276,4402806,4402816,4402817,4403222,4403256,4404937,4404941,4406256,4408340,4408343,4408474,4409820,4410111,4410389,4410877,4410943,4411167,4411176,4412064,4412065,4412066,4412067,4412068,4412069,4412070,4412071,4412073,4412074,4412075,4412076,4412077,4412078,4412079,4412080,4412081,4412082,4412083,4412084,4412085,4412086,4412087,4412088,4412089,4412090,4412091,4412093,4412094,4412095,4412096,4412097,4412098,4412099,4412100,4412101,4412102,4412103,4412104,4412105,4412106,4412107,4412108,4412109,4412110,4412111,4412112,4412113,4412114,4412115,4412116,4412117,4412118,4412119,4412120,4412122,4412123,4412124,4412125,4412126,4412128,4412136,4412137,4412138,4412139,4412140,4412141,4412142,4412143,4412144,4412145,4412146,4412147,4412148,4412149,4412150,4412151,4412153,4412154,4412155,4412156,4412157,4412158,4412159,4412160,4412161,4412162,4412163,4412164,4412165,4412166,4412168,4412169,4412170,4412171,4412173,4412174,4412175,4412176,4412177,4412178,4412179,4412180,4412182,4412183,4412184,4412185,4412187,4412188,4412189,4412190,4412191,4412192,4412193,4412194,4412195,4412196,4412197,4412198,4412199,4412201,4412202,4412203,4412204,4412205,4412207,4412208,4412209,4412210,4412211,4412212,4412213,4412214,4412215,4412216,4412217,4412218,4412219,4412220,4412221,4412223,4412224,4412225,4412226,4412227,4412228,4412230,4412232,4412233,4412234,4412235,4412236,4412237,4412238,4412239,4412240,4412241,4412242,4412244,4412245,4412247,4412248,4412250,4412251,4412252,4412253,4412254,4412255,4412257,4412258,4412260,4412261,4412262,4412264,4412267,4412268,4412269,4412270,4412271,4412272,4412279,4412280,4412281,4412282,4412284,4412285,4412286,4412287,4412288,4412289,4412290,4412291,4412292,4412294,4412295,4412296,4412297,4412299,4412300,4412301,4412302,4412303,4412304,4412305,4412306,4412307,4412308,4412309,4412310,4412311,4412312,4412313,4412314,4412315,4412316,4412317,4412318,4412319,4412320,4412321,4412322,4412323,4412324,4412326,4412327,4412328,4412330,4412331,4412332,4412333,4412334,4412335,4412336,4412337,4412338,4412339,4412340,4412341,4412342,4412343,4412356,4412358,4412359,4412360,4412361,4412362,4412363,4412364,4412365,4412366,4412367,4412368,4412369,4412370,4412371,4412372,4412373,4412374,4412375,4412377,4412378,4412379,4412380,4412381,4412382,4412383,4412384,4412385,4412386,4412387,4412388,4412389,4412390,4412391,4412392,4412393,4412394,4412395,4412397,4412398,4412399,4412400,4412401,4412402,4412403,4412404,4412405,4412406,4412407,4412408,4412409,4412410,4412411,4412412,4412413,4412414,4412415,4412416,4412417,4412418,4412419,4412420,4412421,4412423,4412424,4412425,4412426,4412427,4412428,4412430,4412431,4412432,4412433,4412434,4412435,4412436,4412437,4412438,4412439,4412440,4412441,4412442,4412443,4412444,4412445,4412446,4412447,4412448,4412450,4412451,4412452,4412453,4412454,4412455,4412456,4412457,4412458,4412459,4412460,4412461,4412462,4412463,4412464,4412465,4412466,4412467,4412468,4412469,4412470,4412471,4412472,4412473,4412474,4412475,4412476,4412477,4412478,4412479,4412480,4412481,4412486,4412487,4412488,4412489,4412490,4412491,4412492,4412493,4412494,4412495,4412496,4412497,4412498,4412499,4412500,4412501,4412502,4412503,4412504,4412505,4412507,4412508,4412509,4412510,4412511,4412512,4412513,4412514,4412515,4412516,4412525,4412526,4412527,4412528,4412529,4412530,4412531,4412532,4412533,4412534,4412535,4412536,4412537,4412538,4412539,4412540,4412541,4412543,4412544,4412545,4412546,4412547,4412548,4412549,4412550,4412551,4412552,4412553,4412554,4412555,4412556,4412557,4412558,4412559,4412560,4412561,4412562,4412563,4412564,4412566,4412567,4412568,4412569,4412571,4412572,4412573,4412574,4412576,4412577,4412579,4412580,4412582,4412583,4412584,4412585,4412586,4412587,4412588,4412589,4412590,4412591,4412592,4412593,4412595,4412597,4412598,4412599,4412600,4412601,4412602,4412603,4412604,4412605,4412606,4412607,4412608,4412609,4412610,4412611,4412612,4412613,4412614,4412617,4412618,4412620,4412621,4412622,4412623,4412624,4412625,4412626,4412627,4412628,4412629,4412630,4412631,4412632,4412633,4412634,4412635,4412636,4412638,4412639,4412640,4412641,4412642,4412643,4412644,4412645,4412646,4412647,4412648,4412649,4412650,4412651,4412652,4412653,4412654,4412655,4412657,4412658,4412659,4412660,4412661,4412662,4412663,4412664,4412665,4412667,4412668,4412669,4412670,4412671,4412672,4412673,4412674,4412675,4412676,4412677,4412678,4412679,4412680,4412681,4412682,4412684,4412685,4412686,4412687,4412688,4412689,4412691,4412692,4412693,4412695,4412696,4412697,4412698,4412699,4412700,4412701,4412702,4412703,4412704,4412705,4412706,4412707,4412708,4412709,4412710,4412711,4412712,4412713,4412715,4412716,4412717,4412718,4412719,4412720,4412721,4412722,4412742,4412743,4412744,4412745,4412746,4412747,4412748,4412750,4412751,4412752,4412753,4412754,4412755,4412756,4412758,4412759,4412760,4412761,4412762,4412763,4412764,4412765,4412766,4412767,4412768,4412769,4412770,4412771,4412772,4412773,4412774,4412775,4412776,4412777,4412778,4412779,4412780,4412781,4412782,4412783,4412784,4412786,4412787,4412789,4412790,4412792,4412793,4412795,4412796,4412797,4412798,4412799,4412801,4412802,4412803,4412804,4412805,4412806,4412807,4412808,4412809,4412810,4412811,4412812,4412813,4412814,4412815,4412816,4412817,4412818,4412819,4412820,4412822,4412823,4412824,4412825,4412826,4412827,4412828,4412830,4412831,4412832,4412833,4412834,4412835,4412837,4412838,4412839,4412840,4412841,4412842,4412843,4412844,4412845,4412846,4412847,4412848,4412849,4412850,4412851,4412852,4412853,4412854,4412855,4412856,4412857,4412858,4412859,4412860,4412861,4412862,4412863,4412864,4412865,4412866,4412867,4412868,4412869,4412870,4412871,4412874,4412875,4412876,4412877,4412878,4412879,4412880,4412881,4412882,4412883,4412884,4412885,4412886,4412887,4412888,4412889,4412890,4412891,4412892,4412893,4412894,4412895,4412897,4412898,4412900,4412901,4412902,4412903,4412904,4412905,4412906,4412907,4412908,4412910,4412911,4412912,4412913,4412914,4412917,4412918,4412919,4412920,4412921,4412922,4412923,4412924,4412925,4412926,4412927,4412929,4412930,4412931,4412932,4412933,4412934,4412935,4412936,4412938,4412939,4412940,4412948,4412949,4412950,4412952,4412954,4412955,4412957,4412958,4412960,4412961,4412962,4412963,4412964,4412965,4412966,4412967,4412968,4412969,4412970,4412971,4412972,4412974,4412975,4412976,4412977,4412978,4412979,4412980,4412981,4412982,4412983,4412985,4412986,4412987,4412988,4412989,4412990,4412991,4412992,4412993,4412994,4412995,4412997,4412998,4412999,4413000,4413001,4413002,4413003,4413004,4413005,4413006,4413007,4413008,4413010,4413011,4413012,4413013,4413014,4413015,4413016,4413017,4413018,4413019,4413020,4413021,4413022,4413023,4413024,4413025,4413027,4413028,4413029,4413030,4413031,4413032,4413034,4413036,4413037,4413038,4413039,4413040,4413041,4413042,4413043,4413044,4413045,4413046,4413047,4413048,4413049,4413050,4413051,4413052,4413053,4413054,4413055,4413056,4413057,4413058,4413059,4413060,4413061,4413063,4413065,4413066,4413067,4413068,4413069,4413070,4413071,4413072,4413073,4413074,4413075,4413076,4413077,4413078,4413079,4413080,4413088,4413089,4413090,4413091,4413092,4413093,4413094,4413095,4413097,4413098,4413099,4413100,4413101,4413102,4413103,4413104,4413105,4413106,4413107,4413109,4413110,4413111,4413112,4413113,4413114,4413115,4413116,4413117,4413118,4413119,4413142,4413143,4413145,4413146,4413147,4413148,4413149,4413150,4413151,4413152,4413153,4413154,4413155,4413156,4413159,4413160,4413161,4413162,4413163,4413164,4413166,4413168,4413169,4413170,4413171,4413172,4413173,4413174,4413175,4413176,4413177,4413178,4413179,4413180,4413182,4413183,4413184,4413185,4413186,4413187,4413188,4413189,4413190,4413191,4413192,4413193,4413195,4413196,4413197,4413198,4413199,4413200,4413201,4413202,4413203,4413204,4413205,4413206,4413207,4413208,4413209,4413210,4413211,4413212,4413213,4413214,4413215,4413216,4413217,4413219,4413220,4413221,4413222,4413223,4413224,4413225,4413227,4413228,4413229,4413230,4413231,4413232,4413233,4413234,4413237,4413238,4413239,4413240,4413241,4413242,4413243,4413244,4413245,4413246,4413247,4413248,4413249,4413251,4413252,4413253,4413254,4413255,4413256,4413257,4413259,4413260,4413261,4413262,4413263,4413264,4413265,4413268,4413269,4413270,4413271,4413272,4413273,4413274,4413275,4413276,4413277,4413278,4413279,4413280,4413281,4413282,4413283,4413284,4413285,4413286,4413287,4413289,4413290,4413291,4413292,4413293,4413294,4413295,4413297,4413298,4413299,4413300,4413301,4413302,4413303,4413304,4413305,4413307,4413308,4413309,4413311,4413312,4413313,4413314,4413334,4413335,4413336,4413337,4413338,4413339,4413340,4413341,4413342,4413343,4413344,4413345,4413346,4413348,4413349,4413350,4413351,4413352,4413353,4413354,4413355,4413356,4413357,4413358,4413359,4413360,4413361,4413362,4413363,4413364,4413365,4413366,4413367,4413368,4413369,4413370,4413371,4413373,4413374,4413375,4413376,4413377,4413378,4413379,4413380,4413382,4413383,4413384,4413385,4413386,4413387,4413388,4413389,4413391,4413392,4413393,4413394,4413396,4413398,4413400,4413402,4413403,4413404,4413405,4413406,4413408,4413410,4413411,4413413,4413414,4413415,4413417,4413418,4413419,4413420,4413421,4413422,4413423,4413424,4413425,4413426,4413427,4413429,4413430,4413431,4413432,4413433,4413434,4413436,4413438,4413439,4413440,4413441,4413442,4413443,4413444,4413445,4413447,4413448,4413449,4413450,4413451,4413452,4413453,4413454,4413455,4413456,4413457,4413458,4413460,4413461,4413462,4413464,4413465,4413466,4413467,4413468,4413469,4413470,4413471,4413472,4413473,4413474,4413475,4413476,4413477,4413478,4413479,4413480,4413481,4413482,4413483,4413484,4413486,4413487,4413488,4413489,4413490,4413491,4413492,4413493,4413494,4413495,4413496,4413497,4413498,4413499,4413500,4413501,4413502,4413503,4413504,4413507,4413509,4413510,4413511,4413512,4413513,4413514,4413515,4413516,4413517,4413518,4413519,4413520,4413521,4413536,4413537,4413538,4413539,4413540,4413542,4413543,4413546,4413548,4413549,4413550,4413551,4413552,4413553,4413554,4413555,4413556,4413557,4413558,4413560,4413561,4413563,4413564,4413565,4413566,4413567,4413568,4413569,4413570,4413571,4413573,4413574,4413575,4413576,4413577,4413578,4413580,4413581,4413582,4413583,4413584,4413585,4413587,4413588,4413589,4413590,4413591,4413592,4413593,4413594,4413595,4413596,4413597,4413598,4413599,4413600,4413601,4413602,4413603,4413604,4413605,4413606,4413607,4413608,4413609,4413610,4413611,4413612,4413614,4413615,4413616,4413617,4413618,4413620,4413622,4413623,4413624,4413625,4413626,4413627,4413628,4413629,4413630,4413631,4413632,4413634,4413636,4413637,4413638,4413639,4413640,4413641,4413642,4413643,4413644,4413645,4413646,4413647,4413648,4413649,4413650,4413651,4413652,4413653,4413654,4413655,4413656,4413657,4413659,4413660,4413661,4413662,4413663,4413664,4413665,4413666,4413667,4413668,4413669,4413670,4413671,4413672,4413673,4413674,4413675,4413676,4413677,4413679,4413681,4413682,4413683,4413684,4413685,4413686,4413687,4413688,4413689,4413690,4413691,4413692,4413695,4413696,4413697,4413698,4413699,4413700,4413701,4413702,4413703,4413704,4413705,4413706,4413707,4413708,4413709,4413710,4413711,4413712,4413714,4413715,4413716,4413717,4413718,4413719,4413737,4413738,4413739,4413740,4413741,4413742,4413743,4413744,4413745,4413747,4413748,4413749,4413750,4413751,4413752,4413753,4413754,4413755,4413756,4413757,4413758,4413759,4413760,4413762,4413763,4413764,4413765,4413766,4413767,4413768,4413769,4413770,4413771,4413772,4413773,4413774,4413775,4413776,4413777,4413778,4413779,4413780,4413781,4413783,4413784,4413785,4413786,4413787,4413788,4413789,4413790,4413791,4413792,4413793,4413794,4413795,4413796,4413797,4413798,4413799,4413800,4413801,4413814,4413815,4413817,4413818,4413819,4413820,4413821,4413822,4413823,4413824,4413825,4413826,4413827,4413828,4413829,4413830,4413831,4413832,4413833,4413834,4413835,4413836,4413837,4413838,4413839,4413840,4413842,4413843,4413844,4413845,4413849,4413866,4413867,4413868,4413869,4413870,4413871,4413872,4413873,4413874,4413875,4413876,4413877,4413878,4413879,4413880,4413881,4413882,4413883,4413901,4413902,4413903,4413904,4413905,4413906,4413907,4413908,4413909,4413910,4413911,4413912,4413913,4413914,4413915,4413916,4413940,4413941,4413942,4413943,4413944,4413945,4413948,4413970,4413972,4413973,4413974,4415370,4418814,4418863,4419002,4419770,4419914,4419964,4419974,4419975,4419993,4420040,4420139,4420173,4420174,4420175,4420176,4420177,4420178,4420238,4420256,4420363,4420402,4420407,4420471,4420482,4420532,4420533,4420535,4420536,4420549,4420550,4420620,4420621,4420622,4420623,4420626,4420627,4420870,4421330,4422952,4423272,4423580,4423789,4425155,4425206,4425359,4425573,4425671,4426041,4426316,4426366,4426460,4427159,4427494,4428097,4428281,4428309,4428607,4429037,4429281,4429914,4429943,4430034,4430075,4430087,4430109,4430121,4430147,4430151,4430224,4430360,4430363,4430387,4431951,4433809,4434478,4434608,4434704,4434706,4434708,4434709,4434710,4434712,4434814,4435172,4436942,4436985,4436990,4437431,4437434,4437869,4438291,4438495,4439335,4441063,4441396,4443283,4443503,4444931,4444932,4445222,4445235,4445374,4445380,4445538,4445659,4445661,4447560,4448242,4448295,4449158,4450256,4450302,4450314,4450582,4450662,4450663,4450712,4450746,4451361,4451362,4451364,4451771,4453001,4453140,4455062,4456686,4457328,4459045,4459051,4459489,4459535,4460524,4460589,4461620,4461624,4463372,4463752,4465063,4465349,4466422,4466423,4468385,4468594,4468759,4469429,4471506,4472077,4472078,4472079,4472083,4472085,4472225,4473591,4473988,4474510,4474560,4474626,4474801,4476424,4476631,4476911,4477858,4477895,4478015,4478028,4478071,4478903,4479083,4479092,4479095,4479098,4479103,4479106,4479476,4480131,4480204,4480224,4480850,4481883,4483155,4484808,4485289,4485546,4486956,4487787,4487801,4488930,4488938,4489159,4489940,4490535,4490604,4491051,4491857,4492209,4492213,4492215,4492216,4492217,4492218,4493583,4493585,4494778,4495308,4496396,4496945,4496947,4496953,4496956,4497540,4499031,4499403,4499810,4500247,4500884,4502043,4502045,4502536,4502988,4503207,4503372,4503413,4503414,4503415,4503416,4503417,4503559,4503565,4503566,4503567,4503568,4503632,4503648,4503660,4503672,4503821,4504350,4504359,4504462,4504602,4504771,4504928,4504968,4505098,4505121,4505139,4505145,4505147,4505149,4505150,4505151,4505195,4505204,4505242,4505278,4505311,4505314,4505449,4505473,4505731,4506044,4506564,4506972,4507180,4507266,4507400,4507563,4507564,4507570,4507754,4508009,4508094,4508365,4508553,4508780,4508784,4508845,4509237,4509349,4510036,4510172,4510359,4510447,4510468,4510482,4510564,4510569,4510886,4510994,4511043,4511815,4511878,4512086,4512239,4512628,4513008,4513766,4514654,4514975,4515401,4516286,4516369,4517549,4517832,4518443,4518908,4518968,4518969,4519808,4519811,4519812,4519814,4519815,4522323,4525516,4525900,4525901,4525918,4526636,4526699,4526759,4526761,4526865,4528122,4528184,4530448,4530562,4530882,4531445,4531881,4533206,4534042,4534136,4534696,4537188,4538544,4539111,4539112,4539929,4541219,4541610,4541611,4541685,4541848,4543365,4544451,4545454,4545699,4545906,4546326,4548933,4548935,4549521,4550249,4550250,4550251,4550252,4550253,4550368,4550553,4550564,4550745,4550800,4550801,4550936,4551167,4551897,4552354,4552356,4552563,4552571,4552815,4553143,4553611,4554073,4556708,4557083,4557120,4557235,4557789,4559983,4560170,4562690,4562971,4563059,4563155,4563456,4563681,4564398,4564812,4564853,4564854,4565024,4565328,4565331,4565528,4565611,4566719,4568810,4568814,4568956,4568958,4568963,4569627,4571179,4571246,4571458,4571659,4571729,4571731,4572148,4572791,4572839,4573756,4573977,4574546,4574646,4574653,4576051,4576894,4576970,4579346,4579468,4579472,4579484,4579485,4580173,4580807,4580808,4580809,4582314,4582316,4582659,4583118,4583119,4583374,4585246,4585248,4585250,4585253,4585255,4585428,4585711,4586723,4586724,4586725,4586726,4586727,4586796,4586839,4586849,4586963,4586980,4586982,4587115,4587518,4587823,4588372,4589426,4589772,4589786,4589907,4590254,4591520,4592026,4592034,4592062,4592540,4592625,4592637,4596295,4597308,4597577,4598064,4600780,4600790,4602631,4603576,4606688,4606696,4606982,4607125,4607363,4607488,4607982,4609047,4612147,4613863,4616064,4616066,4616288,4616295,4617821,4618776,4618783,4618855,4619016,4619488,4619841,4619967,4620152,4620237,4621875,4622742,4628673,4628784,4629048,4629792,4631495,4632801,4633639,4633713,4634335,4634337,4634339,4634340,4634341,4634359,4634453,4634928,4634998,4635112,4635113,4635472,4635532,4635663,4635776,4635854,4635883,4635933,4635956,4636859,4636860,4636968,4636970,4637646,4639326,4639747,4641780,4641822,4642974,4643846,4645124,4646695,4646706,4646902,4648861,4652283,4654207,4657202,4657694,4658560,4658782,4658988,4659287,4662309,4664432,4665092,4666801,4670856,4671798,4674214,4674226,4674807,4674884,4674997,4675506,4675526,4678179,4679519,4679933,4680006,4685581,4688537,4689479,4689485,4691732,4692357,4692403,4692742,4692998,4693611,4693613,4695073,4699003,4699675,4699794,4700480,4700766,4702536,4703130,4707571,4710671,4710673,4711098,4711105,4712435,4713452,4713770,4714339,4715117,4715226,4715316,4717554,4719515,4720299,4720305,4722145,4726620,4728281,4729449,4729493,4730150,4730154,4730478,4731388,4733758,4733763,4733765,4740058,4740415,4741415,4741487,4745874,4746294,4750061,4750627,4751333,4753662,4755427,4755852,4755853,4755994,4756627,4757451,4757649,4758937,4758947,4760587,4762198,4762388,4766311,4766315,4766317,4766318,4766322,4766323,4766331,4768115,4768753,4770271,4770762,4772270,4775707,4776933,4778081,4778720,4782980,4784300,4790224,4790268,4790272,4792125,4792230,4792234,4792891,4794142,4794177,4794179,4794180,4794181,4794182,4796009,4796220,4799907,4800062,4801749,4803435,4804111,4806640,4811344,4816658,4818869,4818871,4819168,4821043,4822056,4822057,4824283,4824298,4826700,4826730,4829657,4835368,4836461,4836920,4837770,4840551,4840553,4841417,4841762,4841763,4841767,4842494,4845296,4845371,4845519,4848460,4849709,4851060,4852484,4852487,4853575,4853624,4853774,4853877,4854646,4855175,4856137,4856629,4856842,4857117,4857544,4858602,4858603,4859308,4859346,4859348,4861840,4864673,4865878,4866076,4866172,4868300,4868302,4868305,4871611,4873660,4875895,4877157,4877528,4877772,4878584,4883745,4883913,4885180,4885181,4885339,4886326,4889109,4895487,4896144,4911455,4915182,4915184,4915186,4915187,4916099]:
    #     print(get_sy_orderinfo1({'id': id}))
    queryset = OrderInfo.objects.filter(order_species=3)
    for order in queryset:
        add_orderinfo_index({"id": order.id})
    # print(get_sy_orderinfo({'id': 6272097}))
    # order_pay_transactions_refund()
    # dispose_wxpay_order_result({})
    # order_set = BindCardOrder.objects.filter(id__in=[26855,27275,29662])
    # for order_info in order_set:
    #     if not order_info.customizeactivityflipcards_id:
    #         customizeactivity = order_info.customizeactivity
    #         check_customizeactivityrules_set = customizeactivity.customizeactivityrules_set.filter(
    #             Q(alidiscountcoupon__remain_number__gt=0) | Q(cashactivity__remain_amount__gt=0))
    #         list_data = []
    #         for check_customizeactivityrules in check_customizeactivityrules_set:
    #             probability = int(check_customizeactivityrules.count)
    #             for i in range(probability):
    #                 list_data.append(check_customizeactivityrules.id)
    #         customizeactivityrules_id = random.choices(list_data, k=1)[0]
    #         customizeactivityrules = CustomizeActivityRules.objects.get(id=customizeactivityrules_id)
    #         identify = f'100003{str(datetime.datetime.now()).replace("-", "").replace(":", "").replace(" ", "").replace(".", "")}{"%06d" % randint(0, 999999)}'
    #         instance_data = dict()
    #         instance_data['identify'] = identify
    #         instance_data['customeruservice_id'] = order_info.customeruservice_id
    #         instance_data['mobile'] = order_info.customeruservice.mobile
    #         instance_data['gears'] = 1
    #         instance_data['customizeactivity_id'] = customizeactivity.id
    #         if customizeactivityrules.prize_type in [1, 2]:
    #             instance_data['alidiscountcoupon_id'] = customizeactivityrules.alidiscountcoupon_id
    #             instance_data['win_type'] = customizeactivityrules.prize_type
    #         elif customizeactivityrules.prize_type == 3:
    #             instance_data['cashactivity_id'] = customizeactivityrules.cashactivity_id
    #             instance_data['win_type'] = customizeactivityrules.prize_type
    #         instance = CustomizeActivityFlipCARDS.objects.create(**instance_data)
    #         if instance.win_type in [1, 2]:
    #             instance.open_status = 2
    #             instance.open_time = datetime.datetime.now()
    #             instance.save()
    #             alidiscountcoupon = instance.alidiscountcoupon
    #             if alidiscountcoupon.remain_number <= 0:
    #                 x = list(customizeactivity.customizeactivityrules_set.filter(
    #                     alidiscountcoupon__remain_number__gt=0).values_list('alidiscountcoupon_id', flat=True))
    #                 if x:
    #                     shuffle(x)
    #                     alidiscountcoupon = AliDiscountCoupon.objects.get(id=x[0])
    #                     instance.alidiscountcoupon_id = alidiscountcoupon.id
    #                     instance.save()
    #                 else:
    #                     instance.open_status = 3
    #                     instance.win_type = 4
    #                     instance.open_time = datetime.datetime.now()
    #                     instance.save()
    #                     continue
    #             activity_id = alidiscountcoupon.id
    #             while True:
    #                 alidiscountcoupon = AliDiscountCoupon.objects.get(id=activity_id)
    #                 activity_origin_send_count = alidiscountcoupon.send_count
    #                 activity_origin_remain_number = alidiscountcoupon.remain_number
    #                 activity_new_send_count = activity_origin_send_count + 1
    #                 activity_new_remain_number = activity_origin_remain_number - 1
    #                 if activity_new_remain_number < 0:
    #                     instance.open_status = 3
    #                     instance.win_type = 4
    #                     instance.open_time = datetime.datetime.now()
    #                     instance.save()
    #                     continue
    #                 result = AliDiscountCoupon.objects.filter(id=activity_id,
    #                                                           send_count=activity_origin_send_count).update(
    #                     send_count=activity_new_send_count, remain_number=activity_new_remain_number)
    #                 if result == 0:
    #                     continue
    #                 break
    #             alidiscountcouponinfo_data = {}
    #             alidiscountcouponinfo_data['alidiscountcoupon_id'] = alidiscountcoupon.id
    #             alidiscountcouponinfo_data['level_id'] = instance.level_id
    #             alidiscountcouponinfo_data['status'] = 0
    #             alidiscountcouponinfo_data['template_id'] = alidiscountcoupon.template_id
    #             out_biz_no = f'100004{str(datetime.datetime.now()).replace("-", "").replace(":", "").replace(" ", "").replace(".", "")}{"%06d" % randint(0, 999999)}'
    #             alidiscountcouponinfo_data['out_biz_no'] = out_biz_no
    #             alidiscountcouponinfo_data['gmt_distributed'] = datetime.datetime.now()
    #             alidiscountcouponinfo_data['customeruservice_id'] = order_info.customeruservice_id
    #             alidiscountcouponinfo_data['coupon_channel'] = alidiscountcoupon.coupon_channel
    #             alidiscountcouponinfo, crete = AliDiscountCouponInfo.objects.get_or_create(
    #                 defaults=alidiscountcouponinfo_data, out_biz_no=out_biz_no)
    #             instance.alidiscountcouponinfo_id = alidiscountcouponinfo.id
    #             instance.save()
    #         else:
    #             instance.open_status = 2
    #             instance.open_time = datetime.datetime.now()
    #             instance.save()
    #             cashactivity = instance.cashactivity
    #             cashactivityrule = CashActivityRule.objects.filter(cash_activity_id=cashactivity.id,
    #                                                                is_delete=0).first()
    #             if cashactivityrule.amount > cashactivity.remain_amount:
    #                 instance.open_status = 3
    #                 instance.win_type = 4
    #                 instance.open_time = datetime.datetime.now()
    #                 instance.save()
    #                 continue
    #             amount = cashactivityrule.amount
    #             activity_id = cashactivity.id
    #             while True:
    #                 activity = CashActivity.objects.get(id=activity_id)
    #                 activity_origin_take_amount = activity.take_amount
    #                 activity_origin_remain_amount = activity.remain_amount
    #                 if activity_origin_remain_amount < amount:
    #                     instance.open_status = 3
    #                     instance.win_type = 4
    #                     instance.open_time = datetime.datetime.now()
    #                     instance.save()
    #                     continue
    #                 activity_new_take_amount = activity_origin_take_amount + amount
    #                 activity_new_remain_amount = activity_origin_remain_amount - amount
    #                 result = CashActivity.objects.filter(take_amount=activity_origin_take_amount,
    #                                                      remain_amount=activity_origin_remain_amount,
    #                                                      id=activity_id).update(
    #                     take_amount=activity_new_take_amount, remain_amount=activity_new_remain_amount)
    #                 if result == 0:
    #                     continue
    #                 break
    #             date_now = datetime.datetime.now()
    #             activity_data = {}
    #             activity_data['amount'] = amount
    #             activity_data['create_time'] = date_now
    #             activity_data['cash_activity_id'] = activity.id
    #             activity_data['level_id'] = instance.level_id
    #             activity_data['state'] = 1
    #             identify = random_str()
    #             nonce_str = random_str()
    #             activity_data['identify'] = identify
    #             activity_data['nonce_str'] = nonce_str
    #             activity_data['desc'] = activity.name
    #             activity_data['partner_trade_no'] = f'{date_now.strftime("%Y%m%d%H%M%S")}{"%06d" % randint(0,999999)}{"%07d" % order_info.customeruservice_id}'
    #             activity_data['expired_time'] = date_now + datetime.timedelta(hours=11)
    #             activity_dat, create = CashActivityData.objects.get_or_create(defaults=activity_data, identify=identify)
    #             instance.cashactivitydata_id = activity_dat.id
    #             instance.amount = amount
    #             instance.save()
    #         order_info.customizeactivityflipcards_id = instance.id
    #         order_info.save()
    #         # customeruservice = order_info.customeruservice
    #         # customeruservice.is_psbc_card = 1
    #         # customeruservice.save()
    #     try:
    #         if order_info.business_qrcode and order_info.is_exchange == 0:
    #             business_qrcode = order_info.business_qrcode
    #             instance = MerchantBusinessQRCode.objects.get(sn=business_qrcode)
    #             if instance.is_c_bind == 1:
    #                 inviter_id = instance.customeruservice_id
    #                 inviter_user = instance.customeruservice
    #                 while True:
    #                     inviter_user = CustomerUserVice.objects.get(id=inviter_id)
    #                     origin_point = inviter_user.point
    #                     origin_obtain_point = inviter_user.obtain_point
    #                     new_point = origin_point + Decimal('50')
    #                     new_obtain_point = origin_obtain_point + Decimal('50')
    #                     result = CustomerUserVice.objects.filter(id=inviter_id, point=origin_point).update(
    #                         point=new_point,
    #                         obtain_point=new_obtain_point
    #                     )
    #                     if result == 0:
    #                         continue
    #                     break
    #                 recoad_data = {}
    #                 recoad_data['customeruservice_id'] = inviter_user.id
    #                 recoad_data['operate_type'] = 1
    #                 recoad_data['point_type'] = 3
    #                 recoad_data['point'] = + Decimal('50')
    #                 recoad_data['point_balance'] = new_point
    #                 recoad_data['explain'] = '邀请新用户参与验卡'
    #                 get_point_record_db_model(inviter_user.id).objects.create(**recoad_data)
    #                 order_info.is_exchange = 1
    #                 order_info.save()
    #                 customeruservice = order_info.customeruservice
    #                 customeruservice.is_first = 0
    #                 customeruservice.inviter_id = inviter_id
    #                 customeruservice.inviter_time = order_info.success_time
    #                 customeruservice.save()
    #                 try:
    #                     result = CustomerUserViceInviterRecord.objects.filter(customeruservice_id=customeruservice.id,
    #                                                                           inviter_id=inviter_id).update(
    #                         inviter_time=order_info.success_time, is_inviter=1)
    #                     if result == 0:
    #                         pass
    #                     else:
    #                         instance.inviter_count += 1
    #                         instance.save()
    #                         if inviter_user.wyf_gzh_openid:
    #                             today = datetime.datetime.now()
    #                             activity = CustomizeActivity.objects.filter(activity_type=9,
    #                                                                         available_begin_time__lte=today,
    #                                                                         available_end_time__gte=today).first()
    #                             if activity:
    #                                 inviter_count = CustomerUserViceInviterRecord.objects.filter(inviter_id=inviter_id,
    #                                                                                              inviter_time__gte=activity.available_begin_time,
    #                                                                                              inviter_time__lte=activity.available_end_time).count()
    #                                 if inviter_count in [1, 2]:
    #                                     response_data = {
    #                                         "keyword1": {
    #                                             "value": "已成功邀请1位用户",
    #                                             "color": "#878787"
    #                                         },
    #                                         "keyword2": {
    #                                             "value": "点击立即进入",
    #                                             "color": "#878787"
    #                                         },
    #                                         "keyword3": {
    #                                             "value": "微邮惠商圈",
    #                                             "color": "#ed6f56"
    #                                         },
    #                                         "keyword4": {
    #                                             "value": "查看邀请进度",
    #                                             "color": "#ed6f56"
    #                                         },
    #                                         "keyword5": {
    #                                             "value": "邀请任务完成即可参与抽奖！",
    #                                             "color": "#ed6f56"
    #                                         }
    #                                     }
    #                                     # invite_push_notification(inviter_user.wyf_gzh_openid, response_data)
    #                                 elif inviter_count == 3:
    #                                     response_data = {
    #                                         "keyword1": {
    #                                             "value": "本周已成功邀请3位用户",
    #                                             "color": "#878787"
    #                                         },
    #                                         "keyword2": {
    #                                             "value": "立即点击参与抽奖！",
    #                                             "color": "#878787"
    #                                         },
    #                                         "keyword3": {
    #                                             "value": "微邮惠商圈",
    #                                             "color": "#ed6f56"
    #                                         },
    #                                         "keyword4": {
    #                                             "value": "点击立即进入",
    #                                             "color": "#ed6f56"
    #                                         },
    #                                         "keyword5": {
    #                                             "value": "赢取更多代金券福利！",
    #                                             "color": "#ed6f56"
    #                                         }
    #                                     }
    #                                     # invite_push_notification(inviter_user.wyf_gzh_openid, response_data)
    #                         if instance.inviter_count >= 3:
    #                             cashactivitydata = instance.cashactivitydata
    #                             if not cashactivitydata:
    #                                 companyuser = instance.user
    #                                 companyuser_id = companyuser.id
    #                                 date_now = datetime.datetime.now()
    #                                 nonce_str = random_str()
    #                                 cash_activity_id = 355
    #                                 amount = Decimal('5')
    #                                 cash_activity = CashActivity.objects.get(id=cash_activity_id)
    #                                 cashactivitydata_data = {}
    #                                 cashactivitydata_data['companyuser_id'] = companyuser_id
    #                                 cashactivitydata_data['cash_activity_id'] = cash_activity.id
    #                                 cashactivitydata_data['amount'] = amount
    #                                 cashactivitydata_data['create_time'] = date_now
    #                                 cashactivitydata_data['nonce_str'] = nonce_str
    #                                 cashactivitydata_data['desc'] = cash_activity.name
    #                                 cashactivitydata_data['mobile'] = companyuser.mobile
    #                                 cashactivitydata_data['openid'] = companyuser.openid
    #                                 cashactivitydata_data['appid'] = 'wxb67b94c00d7cd83f'
    #                                 cashactivitydata_data['partner_trade_no'] = f'{date_now.strftime("%Y%m%d%H%M%S")}{"%06d" % randint(0, 999999)}{"%06d" % companyuser_id}'
    #                                 cashactivitydata = CashActivityData.objects.create(**cashactivitydata_data)
    #                                 while True:
    #                                     activity = CashActivity.objects.get(id=cash_activity_id)
    #                                     activity_origin_take_amount = activity.take_amount
    #                                     activity_origin_remain_amount = activity.remain_amount
    #                                     activity_new_take_amount = activity_origin_take_amount + amount
    #                                     activity_new_remain_amount = activity_origin_remain_amount - amount
    #                                     result = CashActivity.objects.filter(
    #                                         take_amount=activity_origin_take_amount,
    #                                         remain_amount=activity_origin_remain_amount,
    #                                         id=cash_activity_id).update(
    #                                         take_amount=activity_new_take_amount,
    #                                         remain_amount=activity_new_remain_amount)
    #                                     if result == 0:
    #                                         continue
    #                                     break
    #                                 instance.cashactivitydata_id = cashactivitydata.id
    #                                 instance.is_achieve = 1
    #                                 instance.award_time = \
    #                                 CustomerUserViceInviterRecord.objects.filter(qrcode_id=instance.id, is_inviter=1)[
    #                                     2].inviter_time
    #                                 instance.save()
    #                 except:
    #                     pass
    #     except:
    #         pass